Maybe you have the same problem I was having: that Auth::user() is null when called from Scope apply() method. Calling it from the constructor does seem to work.. most of the time. I don't understand why it doesn't always work by either method, but so far, by using the constructor and the apply() as backup, it seems to work all of the time.
<?php namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\Auth;
class ShopScope implements Scope
{
protected $user;
/**
* double try to get Auth::user because for unknown reason doesn't always work
*
* ShopScope constructor.
*/
public function __construct()
{
$this->user = Auth::user();
}
public function apply(Builder $builder, Model $model)
{
$user = $this->user ?: Auth::user();
//your code here
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community