F
Filamentβ€’7mo ago
karimakrane

Update my query on my Resource

hello, in my application I have the users, schools and school_user tables which is a pivot table with the is_owner field. now in my SchoolResource I would like to retrieve just the schools with the pivot fields of the authenticated user for this I created a method public function ownedByUser(User $user): Collection { return $this->belongsToMany(User::class, 'school_user') ->wherePivot('is_owner', true) ->wherePivot('user_id', $user->id) ->get(); } or i can create another method in my User model public function ownedSchools(): Collection { return $this->schools() ->wherePivot('is_owner', true) ->get(); } also i'm using force-deletes my question and how can I call it at my SchoolResource thank you
4 Replies
karimakrane
karimakraneβ€’7mo ago
@Lara Zeus like this ???? ->modifyQueryUsing(fn(Builder $query) => $query->ownedByUser(Auth::user())) that s return Call to undefined method Illuminate\Database\Eloquent\Builder::ownedByUser()
Lara Zeus
Lara Zeusβ€’7mo ago
yes, but I think your model not correct?! maybe not sure πŸ™‚ try create a local scope and test it
karimakrane
karimakraneβ€’7mo ago
i@Lara Zeus i do it directly like this public static function getEloquentQuery(): Builder { $user = Auth::user(); return School::query() ->withoutGlobalScopes([SoftDeletingScope::class]) ->whereHas('users', function (Builder $query) use ($user) { $query->where('users.id', $user->id) ->where('school_user.is_owner', true); }); }