• 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 do I upload a laravel project on cPanel shared hosting?

javascript html popup window

Select option menu read from database and use it's values

Saving Data from form to database using AngularJS and php

Laravel query builder binding parameters more than once

Elastic Beanstalk with Laravel Envoy

Issue with laravel eloquent model property

Yii2: How do I debug console commands?

Not able to access model in the controller mautic

CodeIgniter 3 is generating a session file on each request, why?

How to get array values using foreach in laravel

How To Access A Column In M-M Relationship Table In Laravel

how to sort by a custom appended relation to model

Laravel update hasMany relationship using saveMany

Custom Button next to “ADD TO CART” button of WooCommerce based on Product Type

About Contact Privacy policy Terms and conditions