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 you4 Replies
@Lara Zeus like this ????
->modifyQueryUsing(fn(Builder $query) => $query->ownedByUser(Auth::user()))
that s return Call to undefined method Illuminate\Database\Eloquent\Builder::ownedByUser()
yes, but I think your model not correct?! maybe not sure π
try create a local scope and test it
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);
});
}