• 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 to send mail using gmail in Laravel?

How can I get user id from session in javascript?

Modify microseconds of a PHP DateTime object

PHP: Convert any string to UTF-8 without knowing the original character set, or at least try

Class AppHttpControllersUserController Does Not Exist

Call to undefined function openssl_decrypt

Laravel eloquent update record without loading from database

Additional price based on cart item count in WooCommerce

PHP each() function replacement

MySQL upgrade causing unexpected results on simple WHERE clauses

How can I get new CSRF token in LARAVEL by using ajax

Laravel 8 factory state afterCreating

Ajax GET request fails in laravel 8

PHP Form Not Inserting

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

About Contact Privacy policy Terms and conditions