• 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 lang slug in url

Laravel Nova Dependency Container what are the allowed resource properties other than id in dependsOn

Display the default discounted price and percentage on Woocommerce products

How to use $this->session->set_userdata in codeigniter

Laravel 8 factory state afterCreating

Laravel - How to properly generate unique slugs from article titles?

Natural ORDER in Laravel Eloquent ORM

Yii2: How do I debug console commands?

PHP/HTML: Creating A SubMenu

How to Install Composer Require doctrine/dbal

str_word_count() function doesn't display Arabic language properly

Is it possible to re-use an array taken from PHP via JSON in a $.ajax function?

Chunk and transpose a flat array into rows with a specific number of columns

PHP: convert all UTF-8 characters to HTML entities

Creating Combinations of Elements

About Contact Privacy policy Terms and conditions