• 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

Auto increment id JSON

Laravel Excel::store file empty after stored

Symfony 4: how to allow dynamic values for array nodes in bundle configuration?

PHP - installing Xdebug on Mac with XAMPP (Unix File)

How to properly start Laravel 8 with Bootstrap & authentication

Adding reCAPTCHA v2 into my PHP file

Issue with laravel eloquent model property

How do I upload a laravel project on cPanel shared hosting?

Artisan, creating tables in database

Check if user online laravel

How can I convert from a{a{} b{}} to a[a[],b[]] using php preg_match?

PHP Deprecated issue when running artisan command

Laravel Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) on a sub query with "ROW_NUMBER() OVER PARTITION"

Log a user out of a website when they put their computer to sleep

loop through an anchor id

About Contact Privacy policy Terms and conditions