OctoberCMS / Anonymous Global Scope
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);
});
}