• 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

Unable to create lockable file - Laravel 8 & PHP 8

file_put_content...fail to open stream:Permission denied in Laravel 5

Laravel 8 Multiple Relationships for Factory

PHP Download MP3 files from directory on server

Connecting an HTML webpage to a SQL Server

Modify microseconds of a PHP DateTime object

How to disable only_full_group_by option in Laravel

Get number of working days between two dates in PHP [duplicate]

Composer Curl error 60: SSL certificate problem: unable to get local issuer certificate

How to remove from a multidimensional array all duplicate elements including the original?

Shopify password update using Shopify API

Add and update products to session cart in Laravel

Add rows to Single product Additional information table in WooCommerce 3.6

Symfony run hint kernel.secret parameter not found

Undefined variable in Laravel 8 project (php 7.4)

About Contact Privacy policy Terms and conditions