• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

OctoberCMS / Anonymous Global Scope

phplaraveloctobercmsoctobercms-plugins


PHP Snippet 1:

class PluginModel extends Model
{
    /**
     * Scope a query to only records with user.
     */
    public function scopeGetUserRecords($query, $userId)
    {
        return $query->where('user_id', $userId);
    }
}

PHP Snippet 2:

$userRecords = PluginModel::getUserRecords($user->id)->get();

PHP Snippet 3:

protected static function boot()
{
  parent::boot();

  $user = Auth::getUser();
  static::addGlobalScope('user', function ($query) use ($user) {
    $query->where('user_id', $user->id);
  });
}

Related Snippets

Laravel + phpunit + github actions = Failed asserting that '1' is identical to 1

Send POST data via raw JSON with Postman

Error converting docx to pdf using Unoconv

WooCommerce Additional Information - if empty, hide

Last order would be re-order in wooCommerce. How is it possible?

Get image type from base64 encoded src string

How do I pass the dynamic output of a php variable or php function to a CSS variable?

Create a new line whenever an array value reaches more than 10 characters

Problems getting instance of UploadedFile in Yii2

echo language construct is discouraged. (PHP)

laravel sanctum Route [login] not defined

How to properly start Laravel 8 with Bootstrap & authentication

HTML + PHP + PHPMAILER

Laravel multi auth - Authentication user provider [] is not defined

Show only featured products in Woocommerce shop page

About Contact Privacy policy Terms and conditions