• 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 update php.ini on a Mac OS X Mojave?

Correct way to use LIKE '%{$var}%' with prepared statements?

HTML and PHP in one file

php: command not found Command PhaseScriptExecution failed with a nonzero exit code

MongoDB Duplicate Documents even after adding unique key

Laravel relationship belongsToMany with composite primary keys

Warning: Unknown: Failed to write session data using user defined save handler. (session.save_path: ) in Unknown on line 0

PHP: How to solve ob_start() in combination imagepng() issue?

woocommerce change position of price

If action is on a different page do I use fwrite function

Apply filter array/return terms

laravel sanctum Route [login] not defined

Fatal error: Uncaught mysqli_sql_exception: Unknown database 'test' in ... (How do I fix that? Using PHP)

PHP date() with timezone?

Laravel get user data with profile

About Contact Privacy policy Terms and conditions