Recourse list, only show user made entires

Hi, I have a FreelancerCV resource. I want a freelancer to be able to create their own cv items. On the resource list page, I only want to show the entries made by that freelancer. Every FreelancerCV row in the database has a freelancer_id. How can I edit the query with ->where('freelancer_id', $freelancer_id)?
2 Replies
cheesegrits
cheesegrits16mo ago
Modify the query for the resource. Without more info about your model / relationship structure, hard to give you the exact query. But assuming you have a Freelancer model, which itself has a user_id foreign key, and the model you are working on has a freelancer() BelongsTo relationship
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->whereHas(
'freelancer',
fn (Builder $query) => $query->where(
'user_id',
auth()->user()->id
)
);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->whereHas(
'freelancer',
fn (Builder $query) => $query->where(
'user_id',
auth()->user()->id
)
);
}
Although probably best abstract that whereHas() into a local scope on the Freelancer model, and just apply the scope, as you'll almost certainly need it elsewhere.
Luukd_2000
Luukd_2000OP16mo ago
thanks this worked
Want results from more Discord servers?
Add your server