How to get column in Laravel junction table - php

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;

Related

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');
}

Laravel many to many with custom 'ID' column

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.

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

How to get value from column that is in a N-N relationship table in Laravel

Im having a problem. I have two tables, places and cuisines, and cuisine_place and in that table I have a column called default (that shows if that cuisine is the default cuisine for that place). But Im having the problem that Im not able to access to that column.
How can I do?
What I want to do is have them in this answer:
$places = Place::all()->with('cuisines')->withPivot('default');
Something like that.
Thanks
You have a many to many relationship between places and cuisines. That relationship would be defined in the following fashion in your models:
public function cuisines(){
return $this->belongsToMany('Cuisine', 'cuisine_place');
}
The above would be a function in your Place class that references its relationship to the cuisines table. By default, Eloquent will pick up the foreign keys in the cuisine_place table (in your case they would probably be called cuisine_id and place_id). If you want to pick up additional columns from that table on calls to the above relationship function, you can use the withPivot function:
public function cuisines(){
return $this->belongsToMany('Cuisine', 'cuisine_place')->withPivot('default');
}
Now, on calls to the cuisines() method in your Place class, you'll receive the default column in the object associated with that table.
Open documentation for Laravel Eloquent Relationships
And read about Retrieving Intermediate Table Columns
You didn't provide your models code so it's difficult to help you with the exact solution

Categories