• 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

How to set time with DateTime modify?

Group rows by column and sum another column within groups [duplicate]

How to downgrade or install a specific version of Composer?

Json_Encode not returning html even i try to encode JSON_HEX_QUOT | JSON_HEX_TAG

User pool client {id}does not exist

cakephp save drag and drop list order to database

Array to string conversion

Is it possible to change the table name in the migration file-laravel

Image upload not working through ajax Laravel

Use same method for inertia response and json response Laravel Jetstream

Laravel database insert with combining array and string

How to install php yaml on CentOs?

WooCommerce Subscriptions: Remove role on cancelled subscription

Laravel/ PHP: Order By Alphabetical with numbers in order [duplicate]

How to pass data to all views in Laravel 5?

About Contact Privacy policy Terms and conditions