• 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 display a view on laravel 8 and not have a 404 NOT FOUND page?

How to connect to MSSQL Server with PHP from Ubuntu 18.04?

How to display table header in every page using FPDF library?

How to add class name to an existing tag in php

Store multiple fields in JSON column (Nova CMS)

ldap is missing from system when installing adldap2 in laravel

Can't find vendor/autoload.php for Ratchet

Popup Window and PHP form

How to Mock the Request Class in Laravel?

Not able to access model in the controller mautic

Why i get wrong output for html markdown?

SQLSTATE[HY000]: General error: 1 table posts has no column named *

Yii2: How do I debug console commands?

Unable to create lockable file - Laravel 8 & PHP 8

Whats the point of running Laravel with the command 'php artisan serve'?

About Contact Privacy policy Terms and conditions