F
Filamentβ€’2y ago
Vulture

Ambiguous "id" column on Edit Page

Hello, i have a custom query on resource, my problem on edit page, two table with inner joined, both table have "id" column, edit page add "WHERE" query on "id" column, this is causing the error. How to change "WHERE id" to "WHERE users.id" ?

public static function getEloquentQuery(): Builder
{
$authUser = Auth::guard('admin')->user();
//$branch = Branch::where('corp_id',$authUser->corp_id)->first();

if ($authUser->is_owner) {
// NOTE : Inner join bug on this line
return static::getModel()::query()
->select('branches.id AS branch_id','branches.title AS branch_title','users.*')
->join('branches', 'branches.id', '=', 'users.branch_id')
->where('corp_id',$authUser->corp_id);
}

return static::getModel()::query()->where('branch_id',3);
}

public static function getEloquentQuery(): Builder
{
$authUser = Auth::guard('admin')->user();
//$branch = Branch::where('corp_id',$authUser->corp_id)->first();

if ($authUser->is_owner) {
// NOTE : Inner join bug on this line
return static::getModel()::query()
->select('branches.id AS branch_id','branches.title AS branch_title','users.*')
->join('branches', 'branches.id', '=', 'users.branch_id')
->where('corp_id',$authUser->corp_id);
}

return static::getModel()::query()->where('branch_id',3);
}
3 Replies
toeknee
toekneeβ€’2y ago
Why are you using a join opposed to a relationship on the model? If you just create a branches relationship on the users model, you can just called branches.title in the table
Vulture
VultureOPβ€’2y ago
Thank you for solution, I think I'm a little confused πŸ˜ƒ, i use relationship on model now
toeknee
toekneeβ€’2y ago
Ahh no problem, glad to hear it! πŸ™‚
Want results from more Discord servers?
Add your server