F
Filament9mo ago
MAF

How can I work around this `column reference "id" is ambiguous`?

// GradeResource.php

public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery()
->join('schools', 'grades.school_id', '=', 'schools.id')
->select('grades.*', 'schools.name as school_name');
->where('schools.created_by', auth()->id());
}
// GradeResource.php

public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery()
->join('schools', 'grades.school_id', '=', 'schools.id')
->select('grades.*', 'schools.name as school_name');
->where('schools.created_by', auth()->id());
}
3 Replies
cheesegrits
cheesegrits9mo ago
Well, the error is caused by selecting grades.* This is your GradesResource, so the Filament query is already selecting all the grades.* fields. So you could fix it by just removing that from your select(). But really, you should use a relationship. Create a school() BelongsTo on your Grade model ...
public function school(): BelongsTo
{
return $this->belongsTo(School::class);
}
public function school(): BelongsTo
{
return $this->belongsTo(School::class);
}
... and then do ...
public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery()
->whereHas('school', fn (Builder $query) => $query->where('created_by', auth()->id()));
}
public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery()
->whereHas('school', fn (Builder $query) => $query->where('created_by', auth()->id()));
}
That's kind of the point of the Eloquent builder over DB. You don't need to mess around joining stuff by hand. Just use relationships.
MAF
MAF9mo ago
removing that from your select(). did not work, it raised the following error:
Illuminate \ Routing \ Exceptions \ UrlGenerationException
Missing required parameter for [Route: filament.dashboard.resources.grades.edit] [URI: grades/{record}/edit] [Missing parameter: record].
Illuminate \ Routing \ Exceptions \ UrlGenerationException
Missing required parameter for [Route: filament.dashboard.resources.grades.edit] [URI: grades/{record}/edit] [Missing parameter: record].
Anyways, will try whereHas instead of join and update this thread. Thank you @Hugh Messenger whereHas worked perfectly, thank you, again
cheesegrits
cheesegrits9mo ago
N/p
Want results from more Discord servers?
Add your server
More Posts