natcave's avatar
Level 10

Column 'name' in field list is ambiguous with HasManyThrough

I have 3 tables. Let's call them "stores", "categories" and "subcategories".

categories has 3 columns: id, name and store_id

subcategories has 3 columns: id, name and category_id

So, both categories and subcategories have a "name" column.

I'm trying to get all the subcategories related to a store, so I'm using HasManyThrough in the store model:

subcategories() { return $this->hasManyThrough('App\Store\Subcategory', 'App\Store\Category'); }

This make sense, but I'm getting an error:

"Integrity constraint violation: 1052 Column 'name' in field list is ambiguous"

Is there anyway to tell Eloquent I'm only interested in the subcategory name?

Thanks.

0 likes
13 replies
Snapey's avatar

if you are using a select statement, you may need to use tablename.name

4 likes
bwrice's avatar

Do you have the hasMany() relations set up between stores/categories and categories/subcategories?

natcave's avatar
Level 10

Thanks @Snapey

I'm not sure I follow. I'm not using select statements on any of the hasManyThrough relationships. Do you think I need a select statement in this case?

Snapey's avatar

When you get this duplicate name, what are you doing? You only show the relationship above, not what code is calling the relationship.

natcave's avatar
Level 10

Thanks @bwrice

Yes, I do. I'm able to use $store->categories, $category->store and $subcategory->category. But I'd like to generate a list of $store->categories->subcategories, giving me a list of all subcategories related to a store. I know how to write it in MySQL but I was trying to follow the Laravel format. It seems it would work fine if I didn't have ambiguous names in both related tables.

natcave's avatar
Level 10

Hey @Snapey

I'm calling $store->subcategories() in a blade template, specifically in a select drop down so I wrote it as $store->subcategories()->lists('name', 'id'). I get the error in my relationship definition.

Snapey's avatar

In your controller, preload the subcategories with with

$store=Store::with('subcategories')->find(1);

or whatever

then in your select


@foreach($store->subcategories as $subcategory)

    <option value="{{ $subcategory->id }}">{{ $subcategory->name }}</option>

@endforeach 

natcave's avatar
natcave
OP
Best Answer
Level 10

This did the trick:

$store->subcategories()->lists('subcategories.name', 'subcategories.id')

When I remove "subcategories." before "name" (how I had it) I get the ambiguous field error.

Thanks for your help @Snapey and @bwrice. I appreciate it!

Snapey's avatar

lists() was deprecated some time ago in favour of pluck()

1 like
natcave's avatar
Level 10

@Snapey Really? I didn't know. I'll check out pluck now. Thanks for the heads up.

adirgapalit's avatar

in which file/class you put this syntax $store->subcategories()->lists('subcategories.name', 'subcategories.id')?

Firemaps's avatar

Thank you @snapey !!!!!! Remind me to buy you a beer at a future Laracon :D

Please or to participate in this conversation.