Laravel hasMany relation returns ' Not unique table/alias' error

iep

Context

In Laravel/Lumen 5.5 I have a many-to-many relation defined in my model class SchemaTypes, using a pivot tabel schema_parent_type:

public function parents()
{
    return $this->belongsToMany('SchemaParentType', 'schema_parent_type', 'type_id', 'parent_id' );

}

I can attach a new parent without problems using this code:

$type = SchemaTypes::where('name', $parentName)->first();
if ( $type)
{
      $type->parents()->attach($parent->id);
}

When I check the table schema_parent_type after attaching a new 'parent' it looks perfect.

Problem

The problem arises when I want to query for all parents given a specific object. When I use this code:

$type = SchemaTypes::where('name', $parentName)->first();
$parents = $type->parents()->get();

this results in the following error:

SQLSTATE[42000]: Syntax error or access violation: 
1066 Not unique table/alias: 'schema_parent_type' 
(SQL: select `schema_parent_type`.*, `schema_parent_type`.`type_id` as `pivot_type_id`, `schema_parent_type`.`parent_id` as `pivot_parent_id` from `schema_parent_type` inner join `schema_parent_type` on `schema_parent_type`.`id` = `schema_parent_type`.`parent_id` where `schema_parent_type`.`type_id` = 110 and `schema_parent_type`.`deleted_at` is null)

Question

What is the reason that calling $type->parents()->get() produces this error? How can I solve this?

Any comments or answers are welcome.

Ronnie Oosting

You need to change the first argument to another model:

return $this->belongsToMany('SchemaTyp‌​es', 'schema_parent_type', 'type_id', 'parent_id' );

More information can be found here: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias on relationship. Look at David Lartey his note.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Loading more eloquent hasMany relation queries through ajax in Laravel?

How to check hasMany relation record is empty in eloquent laravel

Laravel hasMany relation with custom query

How to get data with hasMany relation and hasMany in laravel?

Creating a hasMany Relation in Laravel 5

Eloquent ORM | Laravel's hasMany (more than 1) relation

Laravel - Eloquent - Filter based on latest HasMany relation

Trying to get property of non-object Laravel hasMany Relation

How to prevent a hasMany relation from loading in Laravel?

How to get hasMany relation in laravel?

Laravel - How to load a hasone relation on a hasmany relation

problem with relation hasmany laravel proprty retro

How to get count of second level hasMany relation through first level of hasMAny in laravel

Laravel relation "sometimes" returns null

Laravel - HasMany relation not loading

Laravel 4: prevent lazy load hasMany relation when foreign_key is null (optional relation)

Laravel 4 Eloquent Problems Select with hasMany Relation

Laravel Eloquent can't get simple hasMany relation working

laravel get relation hasMany model with where clause

Laravel 5.1 - hasMany relation on multiple fields inside one table

Laravel Select the First Row From HasMany Relation

Select and display columns with hasMany relation Laravel

Laravel relationship hasMany returns null

Laravel hasmany function returns nothing

Laravel hasMany to Belongs to returns undefined

HasMany Relation through BelongsToMany Relation

Laravel - How to filter hasMany relation based on parameter?

Sort By date in hasMany relation first record for Laravel project

How to apply hasMany relation from Laravel Model in inertia ReactJs