• 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 array, move keys and values to new array, but mix it up

Laravel Unknown Column 'updated_at'

Laravel csrf token mismatch for ajax POST Request

PHP's array_map including keys

JSON Render Issue in Date Object Laravel and PHP 7.4

Laravel with App Engine Standard Class 'FacadeIgnitionIgnitionServiceProvider' not found

JQuery content editable div and text box on submit not sending POST

How to remove index.php and index from the URL using htaccess | PHP

Laravel display validation error

Laravel phpunit test failing authorization

Issue saving card for customer

Attempt to read property "view" on null when sending password reset email

How can I format this number correctly using PHP?

How to update array value in Laravel

WooCommerce - unset "<product> removed notice…" on cart page

About Contact Privacy policy Terms and conditions