• 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 reset Laravel AuthManager/guards in between API calls in tests?

How to install PHP composer inside a docker container

Method IlluminateAuthRequestGuard::logout does not exist Laravel Passport

How can I pass the list to the component variable in Laravel?

Unable to get password for the instance created from AMI

How to add class name to an existing tag in php

php curl_exec() Connection refused when retrieving a remote image

Timber: Single post pagination does not work (wp_link_pages)

Twig - How to check if variable is a number / integer

Lumen - Postgresql setup - Composer

Add bold text and line break into Laravel Notification Email

php script to delete files older than 24 hrs, deletes all files

Unable to uninstall brew php from homebrew

CodeIgniter 4: Like and Dislike Functionality

How to test a php login connected to a mysql db through xampp?

About Contact Privacy policy Terms and conditions