qWici
1875
1
Laravel

1066 Not unique table/alias

Posted 1 year ago by qWici

Error:*

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'user_beers_data' (SQL: select `user_beers_data`.*, `user_beers_data`.`user_id` as `pivot_user_id`, `user_beers_data`.`id` as `pivot_id` from `user_beers_data` inner join `user_beers_data` on `user_beers_data`.`id` = `user_beers_data`.`id` where `user_beers_data`.`user_id` = 1) 

Beer model

protected $table = "user_beers_data";

protected $fillable = [
    'user_id',
    'beer_name',
    'beer_count',
    'beer_date',
];

public function users()
{
    return $this->belongsToMany(User::class, 'users','id', 'user_id');
}

User model

public function beers()
    {
        return $this->belongsToMany(Beers::class,'user_beers_data', 'user_id','id');
    }

Blade

@foreach($user->beers as $beer)
                    {{$beer->user_id}}
                    {{$beer->beer_name}}
                    {{$beer->beer_count}}
                    {{$beer->beer_date}}
@endforeach 

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.