• 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 5: Is there a non-case sensitive way to sort a collection by an attribute?

Put content of wordpress page inside div

How to read laravel_session cookies saved in cookie memory of browser in client side?

How to add a custom field to all Woocommerce attribute terms using add_action

In PHP, which is faster: preg_split or explode?

How to fix bootstrap multiselect search data using ajax

How to set time with DateTime modify?

Google Gmail API - How to login programatically?

Using array_intersect on a multi-dimensional array

Laravel lang slug in url

PHP how to detect if running on arm64 cpu?

How to properly set "0000-00-00 00:00:00" as a DateTime in PHP

Laravel 5 controller sending JSON integer as string

Laravel Unresolvable dependency resolving [Parameter #0 [ <required> $method ]] in class GuzzleHttpPsr7Request

getting error while enter Command => php artisan route:list

About Contact Privacy policy Terms and conditions