• 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

renameColumn migration fails for existing column with columns does not exist error

Wordpress 'post_type_link' hides permalink

How can i call a function that executes an fpdf based on the choice of a form selector?

Remove categories with all childs derived from parent category

shell errors running php exec

Filter WooCommerce related products by Polylang language

Vimeo API: how to save a vimeo into a subfolder?

Laravel relationship belongsToMany with composite primary keys

Saving Data from form to database using AngularJS and php

PHPUnit (Phar) Fatal Error Only When Test Fails

Laravel 8: Array to string conversion while calling route:list

Zig-zag scan an N x N array

How to delete old images from public folder on update using Laravel

how to fix Service provider class not found when using repository?

How do I remove this delivery notification from here?

About Contact Privacy policy Terms and conditions