• 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

401 Unauthorized only occurring on some pages in Laravel 8

Method IlluminateAuthRequestGuard::logout does not exist Laravel Passport

Set quantity minimum, maximum and step at product level in Woocommerce

Implode array with array of glue strings

Form repeater send wrong data with last element in Laravel

args[max_input] woocommerce if statement confused

Attempted to call an undefined method named "get" of class "MailController"

Access relation of pivot table in Laravel

Detect if PHP session exists

Using spatie/media-library, how to rename a collection of uploaded files?

Passing a boolean value from checkbox in Laravel form

Could not decode a text frame as UTF-8.

Cache clear probem in Larave

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables 11 [closed]

Testing subscription renewals on Stripe

About Contact Privacy policy Terms and conditions