• 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 loop sql table data inside a html table

dompdf and img tag, image wont show

How to fix " Uncaught TypeError: Argument 1 passed to JpGraphException::defaultHandler() " problem in php 7.1.3 and jpgraph 4.2.6?

Check if string contains a value in array [duplicate]

Laravel 8: Array to string conversion while calling route:list

How to set a domain name with php artisan serve

Do I need to install `fxp/composer-asset-plugin` if I'm not doing front-end development

How to send image,audio or video through the WhatsApp API - PHP

Log file is not being written in Laravel 5.5

PHP7 CLI attempts to load xdebug twice - "Cannot load Xdebug - it was already loaded"

Target Laravelista is not instantiable

WooCommerce Additional Information - if empty, hide

Sum array values

Match single unknown parameter php (Morse-code Regex)

is there any way to validate a field that sometime is File(image) && sometime is String(Src of same image)

About Contact Privacy policy Terms and conditions