• 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

PHP cURL (SSL certificate problem: self signed certificate in certificate chain)

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'posts.post' doesn't exist

XAMPP keeps showing Dashboard/Welcome Page instead of the Configuration Page

Android Volley getParams() method not getting called for JsonObjectRequest

php echo xml documents with header

Exact alternate to mcrypt_encrypt in PHP 7.2

Laravel Gates using model and returns "Using $this when not in object context" Line 28 of AzureUser model

PHP only Hit Counter?

How do I refresh a DIV content?

Laravel 8 factory state afterCreating

Call authenticate manually in router middleware

Laravel 5: Is there a non-case sensitive way to sort a collection by an attribute?

Clear javascript source cache laravel 5.8

yii2 and mssql insert varbinary into model

Reload parent page after submit in iframe

About Contact Privacy policy Terms and conditions