Laravel many to many with custom 'ID' column - php

I have 2 tables users and teams, that make a relation table user_team.
The user_team table have cod_user and cod_equipe columns and make references to column cod on users, and cod on teams.
This relation returns NULL
User model
public function user_team(){
//belongs to -> pertence a
return $this->belongsToMany('App\User','user_team','cod_user','cod_equipe');
}
Team model
public function team_user(){
//belongs to -> pertence a
return $this->belongsToMany('App\Team','user_team','cod_equipe','cod_user');
}
I think its because Eloquent try to JOIN cod_user with ID on users, but the reference is on cod column.
How can i change this?

Could you clarify what your database schema looks like regarding these tables? In order to make the relations work when defining them explicitly, you need to specify the foreign key from the model in question, followed by the foreign key of the table you want to join. I am confused by the use of 'cod' here as the foreign key mentioned in both users and teams tables. Is the foreign key reference from the users table a field called 'cod', and the same for teams?
If so, you would need to specify that as the foreign key on your users model, and assuming that 'cod_user' is the key in user_team, it would look like this in the User model:
public function userTeam(){
return $this->belongsToMany('App\User','user_team','cod','cod_user');
}
From the laravel docs:
Laravel 5.3 eloquent many-to-many relationships
In addition to customizing the name of the joining table, you may also
customize the column names of the keys on the table by passing
additional arguments to the belongsToMany method. The third argument
is the foreign key name of the model on which you are defining the
relationship, while the fourth argument is the foreign key name of the
model that you are joining to:
return $this->belongsToMany('App\Role', 'role_user', 'user_id',
'role_id');
Additionally, if the primary key on the users or teams table is not named 'id', then you will have to override the convention by defining the variable $primaryKey in your User and Team models.

Related

How to get column in Laravel junction table

I implemented Many-To-Many Eloquent relationship between products and properties by three table. (like this)
I can access product properties using the following code :
App\Product::find(1)->properties;
but how to get other column in junction table (such as "value" in this case)?
I have attached my database schema image here
Any column that is not a foreign key is not added by default in the relationship object. You need to specify the extra columns you want with withPivot.
# App\Product model
public function properties()
{
return $this->belongsToMany(...)->withPivot('value');
}
To access the intermediate table, use pivot->
App\Product::find(1)->properties->pivot->value;

Laravel: How to add Relationship in Model when my Foreign keys are in array?

I have a table where I need to save the ids in array, you can see the items in
events_who is a foreign keys. Any solution where I can create a Relationship in my Model to get the data from the foreign keys? I tried belongsToMany and it doesn't work. :(
Any suggestions?
Let's assume we are having 2 models: Event and Person
And a Person can participate to multiple Event
Based on the mentioned relationships, you need to create a pivot table called event_person and define two belongsToMany() relationships in both models:
In the Person model, the relationship will look like:
public function events()
{
return $this->belongsToMany(Event::class, 'event_person');
}
Laravel / MySQL (Relational Database) don't really work this way. You should check out Many to Many Relationships in this case.
Example table schema/layout:
users
|id|name|password|
events
|id|title|body|
event_user (pivot table)
|event_id|user_id|
Usually there won't be an array in a column, you should use tables instead. Besides, normally a foreign key would be a table name(singular) following by _id.

One to many relation in pivot table in laravel?

I have pivot table and it has one to many relationship with another table, So I am confused with database design and relationship in Laravel Elequent.
Is this right way to design databse or should I create id as primary key for award_entries and use it is foreign key in entry_files. ?
If I am following existing design (first one), how will I write relation in Laravel eloquent?
Here how your relation will look like. You may need to change model or columns name.
In award_entry model:
public function entry_file() {
return $this->belongsTo('App\Models\entry_file','award_entries_award_id');
}
And in Entry_file model:
public function award_entries(){
return $this->hasMany('App\Models\award_entries','award_entries_award_id');
}

how to define many to many relationship in laravel models?

I have two master tables. i.e, users and tasks. Primary key column names are in_user_id for users table and in_task_id for tasks table.
There is relational table users_tasks (to see a task are assigned to which users and a user is assigned with which tasks). Foreign key columns are in_task_id and in_worker_id in users_tasks table.
I want to define relationship of users and tasks (many-to-many) into both models. I have read this doc. But in my case, primary key column is in_user_id in users table. But foreign key column name is in_worker_id in users_tasks table.
So I don't know that how to handle fourth parameter in relationship function (return $this->belongsToMany('App\User', 'users_tasks', 'in_task_id', ?); in Task Model file). Either in_user_id or in_worker_id or something need to define more. If anyone knows the solution, it will be appreciated.
From laravel eloquent documentation on many-to-many relationships
In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:
In your case it would be :
return $this->belongsToMany('App\User', 'users_tasks', 'in_task_id', 'in_worker_id');
Now this assumes a few things:
Your user and task model are working properly (since you have non-conventional identifiers you've defined them in your models).
Not sure if its still the case but in previous versions of Laravel, your pivot table (in this case users_tasks) also needs it's own identifier column, typically an auto-incremented id.
Try this
$this->belongsToMany('App\User, 'users_tasks', 'in_worker_id ',
'in_user_id ');
params -> model, relation table, foreignkey, primary key
And also relation tables should be in the alphabetical order. (best way)
It must be tasks_users

Laravel - How to use a foreign key to a table with an identifying foreign key?

So I have three tables: users, candidates and logs. Now candidates does not have its own id, it has an identifying foreign key user_id. Looking much like this:
-users-
id
name
etc.
-candidates-
user_id
type
etc.
Now I want to have a table logs that has a one-to-many relation with candidates, being that a candidate can have zero or more logs. So I want to have a foreign key to candidates, something like candidate_id. However, putting it like this and Laravel won't automagically understand the relation. What should I name the foreign key from logs to candidates to make Laravel understand the relationship?
I think the simplest thing would be to give candidates its own incremental id and then have two relationship tables, candidate_user and candidate_log. It would be set up according to Laravel docs
Your candidates table must have a primary key.
There's no need to have an incremental ID, but you shouldn't leave the table without a primary key.
Once you define a primary key, you can use it as your reference.
Laravel will try to guess your foreign_key name:
https://laravel.com/docs/5.2/eloquent-relationships#one-to-many
Remember, Eloquent will automatically determine the proper foreign key column on the Comment model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id. So, for this example, Eloquent will assume the foreign key on the Comment model is post_id.
If your primary key is not the "id" column, you should use the protected $primaryKey and the public $incrementing properties to describe your primary key in your Eloquent Model.

Categories