How to make a relation in laravel with multiple pivot tables? - php

I got a tables:
users (id, name, surname)
weddings (id, ...)
wedding_user (id, wedding_id, user_id, role_id)
wedding_user_permission (wedding_user_id, permission_id)
And I want to make a relation between Wedding and WeddingUser model (Wedding has many WeddingUser, WeddingUser belongs to Wedding).
Actually, if I'll ignore laravel relations, model will be:
//Wedding Model
public function users()
{
$users = WeddingUser::where('wedding_id', $this->id)->get();
return $users;
}
But I need to do this with the laravel relations.
I tried this:
public function users()
{
return $this->belongsToMany(
'App\Models\WeddingUser',
'wedding_user',
'user_id'
);
}
But it throws an error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'wedding_user' (SQL: select `wedding_user`.*, `wedding_user`.`user_id` as `pivot_user_id`, `wedding_user`.`wedding_user_id` as `pivot_wedding_user_id` from `wedding_user` inner join `wedding_user` on `wedding_user`.`id` = `wedding_user`.`wedding_user_id` where `wedding_user`.`user_id` in (1))
How to do it correctly?

Related

Why is the request not working correctly when using BelongsToMany Laravel?

I have 2 models in my app, 'Subject' & 'Professor' (each Subject belongs to many Professors).
I made the many-to-many relation between two model using belongsToMany(). belongsToMany() doesn't work.
I'm trying to get data like this:
$subjects = Subject::with(["professors"])->whereHas("professors", function ($q){ $q->where("id", \request("professor_id")); })->get();
Error:
"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `subjects` where exists (select * from `professors` inner join `subjects_of_professor` on `professors`.`id` = `subjects_of_professor`.`professor_id` where `subjects`.`id` = `subjects_of_professor`.`subject_id` and `id` = 39))",
Does anyone know where did I make a mistake?
Here's the code to Models:
class Subject extends Model
{
public function professors(): BelongsToMany
{
return $this->belongsToMany(Professor::class, "subjects_of_professor");
}
}
class Professor extends Model
{
public function subjects(): BelongsToMany
{
return $this->belongsToMany(Subject::class, "subjects_of_professor");
}
}
And here is my database structure:
subjects:
id
title
subjects_of_professor:
id
subject_id
professor_id
professors:
id
name
description
I'm found mistake, i was needed to add table in my code, when i trying to get data:
$subjects = Subject::whereHas("professors", function ($q){ $q->where("professors.id", \request("professor_id")); })->get();

Laravel Eloquent fail union between two relations

I have two tables, users and projects and another table called users_project used to perform many to many relation.
Users columns: id, firstname, lastname, ... (others not important for this post)
Projects columns: id, commercial, name, ... (others not important for this post)
Users_project: user_id, project_id
In my User model class I have two methods:
public function commercial_projects() {
return $this->hasMany('App\Project', 'commercial', 'id');
}
and
public function assigned_projects() {
return $this->belongsToMany(
'App\Project',
'users_project',
'user_id',
'project_id'
);
}
This methods works fine.
The first return how many projects have the user involved as commercial user
The second return how many projects are assigned to user
I want to have another method that return all project where user are involved in (commercial or assigned)
I wrote this method as follow:
public function projects() {
return $this -> assigned_projects() -> union( $this->commercial_projects() );
}
But I have the error caused by pivot column added by belongsToMany relationship:
"SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select `projects`.*, `users_project`.`user_id` as `pivot_user_id`, `users_project`.`project_id` as `pivot_project_id` from `projects` inner join `users_project` on `projects`.`id` = `users_project`.`project_id` where `users_project`.`user_id` = 13) union (select * from `projects` where `projects`.`commercial` = 13 and `projects`.`commercial` is not null))"
How can I remove pivot column from database request or find a way to fix this issue?
Tnx a lot
I think you need to select only projects columns of assigned_projects
->select("projects.*")
To be like this
return $this->assigned_projects()->select("projects.*")->union($this->commercial_projects());

How can I get this many-to-many relationship work in Laravel 5.5?

I am getting an error with a many to many relationship in eloquent in Laravel 5.5
Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users' (SQL: select users.*, users.roles_id as pivot_roles_id, users.users_id as pivot_users_id, users.id as pivot_id, users.created as pivot_created, users.updated as pivot_updated, users.deleted as pivot_deleted from users inner join users on users.id = users.users_id where users.roles_id = 2)
I have a users table, roles table and a user_role table (pivot table). The user_role table has the following columns: id, users_id(fk from users table),roles_id (fk from roles table),created,updated,deleted.
In the User model I have
public function roles(){
return $this->belongsToMany(\App\roles::class,'roles','users_id','roles_id','id','id')->withPivot(['id','created','updated','deleted']);
}
In the roles model I have
public function users(){
return $this->belongsToMany(\App\User::class,'users','roles_id','users_id','id','id')->withPivot(['id','created','updated','deleted']);
}
In my controller I have
$roles = $this->rolesObject->whereNull('deleted')->orderBy('role')->get();
//Just for testing how to view the relationship won't be real code in the end
foreach($roles as $role){
foreach($role->users as $user){
dump($user->display_name);
}
}
Looking through the documentation and it looks like i am setting up the relationship correctly but obviously I'm not and I have no idea what I am doing wrong
The second parameter to belongsToMany method is a relation table name(docs), you have to pass role_user rather than users.
public function roles(){
return $this->belongsToMany(\App\roles::class,'role_user','users_id','roles_id','id','id')->withPivot(['id','created','updated','deleted']);
}
...
public function users(){
return $this->belongsToMany(\App\User::class,'role_user','roles_id','users_id','id','id')->withPivot(['id','created','updated','deleted']);
}
Your code thinks that your second parameter users is a relation table name, that's the reason of the error.

Laravel 5.3 hasManyThrough through an intermediate table

I have the following table relationship:
organizations
id - integer
organization_users
organization_id - integer (FK)
user_id - integer (FK)
users
id - integer
I am trying to get all the users of an organization through eloquent relationships. Here is my Organization.php model with its relationships:
class Organization extends Model
{
public function Users(){
return $this->hasManyThrough('App\User', 'App\OrganizationUser',
'organization_id', 'user_id', 'id');
...
}
I have tried many combinations of that relationship such as
return $this->hasManyThrough('App\User', 'App\OrganizationUser',
'user_id', 'organization_id', 'id');
But all turn up somewhat the same error (this one is from the first query):
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not
found: 1054 Unknown column 'organization_users.id' in 'on clause' (SQL: select
`users`.*, `organization_users`.`organization_id` from `users` inner join
`organization_users` on `organization_users`.`id` = `users`.`user_id` where
`organization_users`.`organization_id` = 1)'
Is it possible that I can have the relationship retrieve the user_id to query on the users table instead of Laravel trying to retrieve organization_users.id? If not is there another way around this?
This is many to many relationship.
User Model:
public function organizations()
{
return $this->belongsToMany('App\Organization','organization_users');
}
Organization Model:
public function users()
{
return $this->belongsToMany('App\User','organization_users');
}
To, get all the users with their organizations:
$users=User::with('organizations')->get();
foreach($users as $user)
{
print_r($user->name);
foreach($user->organizations as $organization)
{
print_r($organization->name);
}
}
What you are describing looks like it may be a 'Many-to-Many' relationship.
https://laravel.com/docs/5.3/eloquent-relationships#many-to-many

Create complex hasManyThrough Laravel 4 query

I have classes User, User_Role and Role_Permission with correspondent tables
users (id)
users_roles (user_id, role_id, one user_id has one role_id)
roles_permissions (role_id, permission_id, one role_id has many permission_id)
I want to get permissions for user via $user->permissions. For this I wrote function
public function permissions()
{
return $this->hasManyThrough('User_Role', 'Role_Permission', 'user_id','role_id');
}
I get the error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles_permissions.user_id' in 'field list' (SQL: select `users_roles`.*, `roles_permissions`.`user_id` from `users_roles` inner join `roles_permissions` on `roles_permissions`.`id` = `users_roles`.`role_id` where `roles_permissions`.`user_id` = 1)
What am I doing wrong? Thanks!
Switch the order of the first two arguments:
public function permissions()
{
return $this->hasManyThrough('Role_Permission', 'User_Role', 'user_id', 'role_id');
}
Eloquent is looking for the user_id column in the Role_Permission table, which it won't find..

Categories