searching a column form pivot table - php

I have 3 models Eg: Model-1, Model-2, Model-3.
Model-2 and Model-3 has many to many relationship,
So i'm keeping one pivot table like Model-2_Model-3.
Model-1 is has one relationship with model-2 and i'm listing all the data from model-1,
I need to search one column from pivot table for listing Model-1.
For Eg:
$data = Model-1::with('Model-2')->get();
I need to search Model-1 Using pivot table (Model-2_Model-3).
I return pivot connection code already. Pivot is working very well.

You can query pivot table with wherePivot()
Model-1::with('Model-2')->wherePivot('field_name', $value)->get();
If the pivot values are not added, you need to add it as withPivot('field_name')

You may like to use contains, as for many to many relationship you will get collection of second model something like $user->roles, so here you can check if $user->roles->contains('field_name'). as well as $user->roles()->wherePivot('role_id', 1)->get()->isEmpty() or $user->roles()->wherePivot('role_id', 1)->get() depending on your requirement.

Related

How To Bulk Update Pivot Table For Same Attributes In Laravel Many To Many

I just found out that when i'm using sync() method for updating pivot table from many to many table, it was executing multiple query.
In my condition, i just trying to update same attributes for all related pivot table records.
This is my script :
foreach ($serials as $serial) {
$newSerials[$serial['id']] = ['warehouse_id' => 1];
}
$record->serials()->sync($newSerials)
Are there any other solution that can do update in pivot table in only one query shot in laravel with Eloquent way ?
I'm pretty sure Query Builder can handle this, but what i need to know in is Eloquent way, that maybe can combine with sync() method, because i alreay implemented sync() to the most of similar case like this in my project.
yes you can, using newPivotQuery() which make the query run directly on pivot table,
you should prepare the array you want to add, give it key value pairs according to your pivot fields names, something like:
foreach ($serials as $serial) {
$newSerials[] =
['serials_id'=>$serial->id,'record_id'=>$record->id,'warehouse_id' => 1];
}
$record->serials()->newPivotQuery()->insert($newSerials);
and there is another way, you can create model for the pivot table by inheriting from pivot, more details in:
https://laravel.com/docs/7.x/eloquent-relationships#defining-custom-intermediate-table-models
i must note that this code insert $newSerials, not updating them, if you want to update the record, method sync() is you best choice

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.

How to get inserted id of pivot when I use attach() in Laravel 5.6 (Eloquent)?

I have this tables: users, programs and users_programs (pivot)
I use this sentence to relate users with programs (Many to Many)
$user->programs()->attach($program);
I want to get id to pivot table users_programs, it's posible? How I return id when save a row?
Thanks
Pivot tables don't have an increment id field in the realm of Eloquent.
If you want to get a specific program for a user in the pivot table, you can use something like the following approach as an example:
$user->programs()->attach($program->id);
$user = $user->fresh();
$userProgram = $user->programs->keyBy('program_id')->get($program->id);

How to bulk-select only pivot tables using Eloquent?

I have two models with many-to-many relationship defined between them: User and Post. I need to access all of the user's posts' pivot table records. How can I achieve this? For example:
$user->posts()->pivot()->get(); // I need to do something like this
The reason why I need this, is to perform a bulk-update in one query on those pivots. Illustration example:
// set 'times_seen' to 100 for all of the pivot records
$user->posts()->pivot()->update(['times_seen' => 100]);
Your question is not really clear, I'm not sure what you really need so please provide more details.
Data from pivot table is available in $pivot attribute of related model, so you can access it with the following:
foreach ($user->posts as $post) {
echo $post->pivot->someFieldFromPivotData;
}
You can find more info on how to work with pivot tables in the docs: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
The most efficient way to update all related pivot rows is to directly operate on that table:
// to set to fixed value
DB::table('pivot_table_name')->where('user_id', $user->id)->update(['times_seen' => 1]);
// to increment by one
DB::table('pivot_table_name')->where('user_id', $user->id)->increment('times_seen');
The pivot table usually has just two foreign keys used for connection of two models. If you need to get all posts of specified user, you can do this with $user->posts().
I want to bulk-update the pivot records in one sql query. Iterating through each one of them would be inefficient
If understood you correctly, you want to use attach(), detach() and sync() with an array:
$user->roles()->detach([1, 2, 3]);
$user->roles()->attach([1, 2, 3]);
$user->roles()->sync([1, 2, 3]);
https://laravel.com/docs/5.1/eloquent-relationships#inserting-many-to-many-relationships

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