auth()->check is false

Hi, I have a simple Trait in my Filament V3 app.
<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait BelongsToTeam
{
public static function bootBelongsToTeam()
{
dd(auth()->check());
if (auth()->check()) {
static::creating(function ($model) {
$model->team_id = auth()->user()->team->id;
});

static::addGlobalScope('team_id', function (Builder $builder) {
return $builder->where('team_id', auth()->user()->team->id);
});
}
}
}
<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait BelongsToTeam
{
public static function bootBelongsToTeam()
{
dd(auth()->check());
if (auth()->check()) {
static::creating(function ($model) {
$model->team_id = auth()->user()->team->id;
});

static::addGlobalScope('team_id', function (Builder $builder) {
return $builder->where('team_id', auth()->user()->team->id);
});
}
}
}
auth()->ckeck is false, but why? Any idea?
12 Replies
Dennis Koch
Dennis Koch2y ago
Where are you using this trait? It probably runs before any Middleware, so there is not authenticated user yet
Soldges
SoldgesOP2y ago
I am using it the default Laravel User Model.
Dennis Koch
Dennis Koch2y ago
Yeah and the user model is booted before any middleware
Soldges
SoldgesOP2y ago
hmmm.. Okay. Then I have to try this solution: https://filamentphp.com/docs/3.x/panels/tenancy#simple-one-to-many-tenancy I thought it would be better and more flexible in a Trait than directly in the model
Soldges
SoldgesOP2y ago
middleware auth should be booted before user
Dennis Koch
Dennis Koch2y ago
Why do you need the auth()->check() outside of the Closures at all? Move it to the closures if you need it and everything should be fine. Very unlikely 😉
Soldges
SoldgesOP2y ago
That was just my example. Here is the code and it is inside the closure but still false public static function bootBelongsToTeam() { if (auth()->check()) { static::creating(function ($model) { $model->team_id = auth()->user()->team->id; }); static::addGlobalScope('team_id', function (Builder $builder) { return $builder->where('team_id', auth()->user()->team->id); }); } }
Dennis Koch
Dennis Koch2y ago
Line 3: if (auth()->check()) { it's not inside the closure. It's the first statement in a boot() method.
Soldges
SoldgesOP2y ago
Ok, I will have a look at it.
Soldges
SoldgesOP2y ago
In the docs https://filamentphp.com/docs/3.x/panels/tenancy#simple-one-to-many-tenancy It it also outside the closure use Illuminate\Database\Eloquent\Builder; class Post extends Model { protected static function booted(): void { if (auth()->check()) { static::addGlobalScope('team', function (Builder $query) { $query->where('team_id', auth()->user()->team_id); // or with a team relationship defined: $query->whereBelongsTo(auth()->user()->team); }); } } }
Dennis Koch
Dennis Koch2y ago
Hm. Not sure if that's right.
Soldges
SoldgesOP2y ago
Maybe the problem is that I add the trait to the user model and the traits tries to fetch the authenticated user but for this it needs to boot the user model and thats an loop... Hmm... In my other plain laravel 10 app without Filament it works without any problem

Did you find this page helpful?