• 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

Remove categories with all childs derived from parent category

Yii Ajax Submit button

Creating Combinations of Elements

Install mysql client in docker image

HTML and PHP in one file

Passing a boolean value from checkbox in Laravel form

laravel 5.6 bulk inserting json data

Laravel database insert with combining array and string

How to MODIFY a Google Docs document via API using search-and-replace?

How to fix this error : Ask your hosting provider to use PHP 7.2.5 or higher for both HTTP and CLI?

How to Install Composer Require doctrine/dbal

array_map triple dimensional array [duplicate]

array_key_exists(): The first argument should be either a string or an integer

Limit login attempts in Laravel 5.7

Symfony Mercure "Failed to send an update"

About Contact Privacy policy Terms and conditions