• 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 phpunit test failing authorization

(Laravel) How to delete multiple models including optional relationships?

how to insert data into select2 search input after scan using qrcode

How to pass data to all views in Laravel 5?

Laravel csrf token mismatch for ajax POST Request

How to create a individual template for page or post in custom plugin?

Wordpress 'post_type_link' hides permalink

Only on Firefox "Loading failed for the <script> with source"

get attribute from class not working php 7

Installing Composer - Internal Error

Find out the name of the last script that included the current one

PHP Startup Unable to load dynamic library /usr/lib/php/20151012/php_mysqli.dll

How to check user Permissions using Custom Middleware in Laravel

Issue with laravel eloquent model property

How to get company posts from LinkedIn via API?

About Contact Privacy policy Terms and conditions