• 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 get company posts from LinkedIn via API?

Is there a way to use Foundry Model for Authentification inside Functional Tests?

Laravel s3 upload file with metadata using pre-signed url

How to get rid of ampersand using WP Nonce URL and WP Redirect or PHP header

Using array_intersect on a multi-dimensional array

Laravel Sanctum CSRF not authenticating

Laravel-fopen: failed to open stream: Permission denied

Android Volley getParams() method not getting called for JsonObjectRequest

How update php.ini on a Mac OS X Mojave?

How to make a array inside array?

PHP 8.1: strftime() is deprecated

phpunit - mockbuilder - set mock object internal property

Sentry + Laravel: how to log an already catched Exception?

PHP: convert all UTF-8 characters to HTML entities

Center point of multiple gps coordinates with php

About Contact Privacy policy Terms and conditions