Logo

dev-resources.site

for different kinds of informations.

Avoid Ambiguous Column Eloquent Query Exception in Laravel

Published at
10/14/2020
Categories
laravel
eloquent
relationships
Author
arielmejiadev
Categories
3 categories in total
laravel
open
eloquent
open
relationships
open
Author
13 person written this
arielmejiadev
open
Avoid Ambiguous Column Eloquent Query Exception in Laravel

Scenario: you have some relationship and you want to get only a few columns.

Models:

User:

public function teams()
{
    return $this->belongsToMany(Team::class);
}
Enter fullscreen mode Exit fullscreen mode

Team

public function users()
{
    return $this->belongsToMany(User::class);
}
Enter fullscreen mode Exit fullscreen mode

Then you can attach users to teams like this:

Team::users()->attach(auth()->user());
Enter fullscreen mode Exit fullscreen mode

and now you can get a collection of users by teams like:

$users = Team::users;
Enter fullscreen mode Exit fullscreen mode

Ok here all fine, maybe you need to pass data to an API or just to a view but, User model has sensitive data or maybe your User model is huge and its a better approach to get only the data that you need, maybe you want the name and email only, you are probably doing something like this:

$team = Team::first();
$team->users()->select(['name', 'email'])->get();
Enter fullscreen mode Exit fullscreen mode

Here you would see some eloquent exception, this is because the User model has a column name and Team model could have a column name too, but dont worry like all in Laravel is really easy, just be explicit with the table and the columns that you need:

Team::users()->select(['users.name', 'users.email'])->get();
Enter fullscreen mode Exit fullscreen mode

The same idea apply when you need to add a "where" method:

Team::users()->where('users.email', $request->get('email'))->get();
Enter fullscreen mode Exit fullscreen mode
relationships Article's
30 articles in total
Favicon
Easier Relationship Mapping in the Backstage Catalog
Favicon
Understanding Self-Relationships in Laravel Models: A Simple Guide
Favicon
Kamagra 50 mg: Revitalize Your Love Life
Favicon
Turk Love Unveiled: A Journey to Lasting Joy in Relationships
Favicon
I want a divorce
Favicon
How to delete data from one to many relationship in Laravel?
Favicon
How to update a one-to-many relationship in Laravel?
Favicon
How can you retrieve data from a one-to-many relationship in Laravel?
Favicon
How to insert data in one to many relationship in database?
Favicon
How to create a One-To-Many relationship in Laravel?
Favicon
How to delete data from one to one relationship in Laravel?
Favicon
How to update a one-to-one relationship in Laravel?
Favicon
How can you retrieve data from a one-to-one relationship in Laravel?
Favicon
How to insert data in one to one relationship in database?
Favicon
How to create a One-To-One relationship in Laravel?
Favicon
What types of relationships are there in Laravel?
Favicon
HasOne Through Relationship
Favicon
How to run Monica personal CRM on Dokku
Favicon
Building Deep Relationships
Favicon
Why do I like to learn?
Favicon
The story of my year
Favicon
The lost message
Favicon
The TRUTH about sexy; hot questions with real answers! Podcast
Favicon
My memory of the time in secondary school
Favicon
What money cannot buy
Favicon
A love that not meant for me
Favicon
Avoid Ambiguous Column Eloquent Query Exception in Laravel
Favicon
Self-Referential m:n Relationships
Favicon
Ways to declare unsigned columns in the Laravel migrations
Favicon
Schema Design: Basic Tables & Relationships

Featured ones: