Eloquent relationships syncing - php

I'm struggling with Eloquent relationships in my Laravel project.
I have a 'user' table and a 'user_lines' table in my database. The 'user_lines' table has a' user_id' field that corresponds to the 'id' field in 'users', in a classic master-detail relationship.
The User model has a hasMany relationship to the UserLine model. The UserLine model has a belongsTo relationship to the User model.
In my application I have a user form that contains several userlines. During form submits those userlines can be added, altered or deleted. So far I find myself iterating the submitted userlines and manually creating, updating and deleting the userlines. I feel I'm missing out on the attach/detach/sync methods Eloquent provides but I can't seem to get them to work.
$user->lines()->attach or $user->lines()->sync
gives me an exception telling me these are undefined methods on Illuminate\Database\Query\Builder.
$user->lines()->createMany($lines)
works but that just creates new lines, it does not update existing lines and delete removed lines.

The attach, detach and sync methods are for the many-to-many relationship and not for one-to-many. So, you need to iterate over lines and add or edit them one by one.

Your tables relationships isone-to-many and for attaching to such relationships you should use save, saveMany or create methods.
for updating you could do something like this:
$user->lines()->where('field',$something)->update($array);
The attach, detach and sync methods are for the many-to-many relationships and not for one-to-many.

Related

Laravel insert into many to many relation table [duplicate]

A simple and straight one:
How can I attach or detach new records when using a Laravel hasManyThrough relation the Laravel way?
Model retrieving is obvious, from the docs here.
EDIT: In other words, is there a Laravelish way of doing the following smarter (model names taken from docs)?
$user = $country->users()->first;
$post->user_id = $user->id;
$post->save();
Thanks in advance.
hasManyThrough() requires an existing intermediate relationship. In the docs example, User is the intermediate relationship. Directly attaching a Post to a Country is not possible because it doesn't know which User owns it. You need to first attach it to the User.
https://laravel.com/api/5.8/Illuminate/Database/Eloquent/Relations/HasManyThrough.html
Yes, but you have to specify all the params.
firstOrNew
// (Doesn't persist to DB, you have to manually call the save method later on the created model)
updateOrCreate
// (Persists to DB)
rawUpdate
// (Persists to DB, not recommended)
push is supposed to work too, according to the docs.
On a 1:M relationship, there's the save method available out of the box. That's because the only field Laravel has to fill in is the foreign key.
For example, Let's say you've got a Parent and Child models. When you call
$parent->children()->save(new Child(...));
Laravel fills in the foreign key and persists the model
If we had a GrandParent model as well, and we tried to save a child through a HasManyThrough relationship:
$grandparent->grandchildren()
Laravel would not only have to fill in for the Parent foreign key, but maybe even create a new Parent model as well since we're not sure it exists. That's why there's not a save method implemented.
Therefore, you can make something like
$grandparent->grandchildren()->firstOrNew(['parent_id' => $parent_id])->save();
// Or
$grandparent->grandchildren()->updateOrCreate(['parent_id' => $parent_id]);
You need a valid key too or else you'll get a SQL constraint violation.

establish a relationship among a table with many different table

i have a table (piovt table) that links the invoice id and the service id, but the service id does not come from one table but comes from different tables. here is an example of that.
i tried use polymorphic relations but i only get the last recored not all recoreds related to the invoice
so how to get the services from invoice table?
You can check the Has One Through relationship.
Laravel documentation states "The "has-one-through" relationship defines a one-to-one relationship with another model. However, this relationship indicates that the declaring model can be matched with one instance of another model by proceeding through a third model."
You can read more and get code examples here
Laravel has one through relationship

Laravel Eloquent Model can't sync with pivot data in Many-to-Many relationship

I would like to sync the data instead of attach the data to the particular relationship.
Pivot relation UserModel code
public function carts(){
return $this->belongsToMany(Product::class,'user_carts')->withPivot('quantity');
}
The attach code is
User::find(1)->carts()->attach($s,["quantity"=>1]);
The sync code is
User::find(1)->carts()->sync($s,["quantity"=>1]);
When I try to compile the sync, those pivot relation that matched user_id = 1 does not have the "1" in its respective quantity column.
If I would like to achieve the sync function without using attach, how can I do it because the attach() will create multiple redundant data in my database.
You have to pass key values in the sync method.
Assuming $s is the id (key) to be synced:
User::find(1)->carts()->sync([$s => ["quantity"=>1]]);

Fetching all pivot table columns via custom pivot type

I have a many-to-many relationship where the pivot table has about 20 additional columns. I am using a custom pivot class, and I have successfully set up the code to return an instance of that class when the ->pivot property is accessed on the relation, e.g.
$supplier->products->pivot returns the custom pivot class.
However, when wanting to access the data, I can manually define all the individual attributes of the pivot class (which extends Pivot by the way) in the belongsToMany relationship like this:
return $this->belongsToMany(Product::class, ['prop1', 'prop2', 'prop3'])
...But, how can I retrieve all the pivot data of the class without manually defining them as it ties the relationship declaration very close to the class? Is this possible. If not, it's going to make maintainability a PITA! Ideally, it'd be really nice if withPivot just had a flag to get it all!
In my circumstances, I found it easier to separate all the data into a separate table and model, and add a foreign key in the pivot table to the additional table record. This allows me to use the 'normal' model handling in Laravel and means I don't have to mess around with problems like this!
My use case was a schema of product and supplier with a many-to-many, and each supplier having their own data for the product, namely price, stock, shipping cost / times etc, so I moved all this from the pivot to a SupplierProduct model.
I'll leave this question here, as although this isn't the direct answer to the question (which I fear the answer is 'no'), this is a solution which is viable and can save quite a bit of coding frustration!

Difference between HABTM relationship and 2 $belongsTo relationship with a third model

I'm creating a project management system which projects are assigned to users
What's the difference between creating a Model ProjectsUser and defining 2 $belongsTo relationship and defining HABTM relationships in both Project and User models? What would be the most correct way, though? And how do I save the data in the projects_users table?
From my experience, if you want to be able to save or delete rows only from the join table (the one with 2 IDs), then it is much more simple using three models associated through both a hasMany and a belongsTo association.
You can also retrieve data from the join table directly and do the queries you want much more easily
This is what CakePHP documentation says refering to HABTM and saving data:
However, in most cases it’s easier to make a model for the join table and setup hasMany, belongsTo associations as shown in example above instead of using HABTM association.
Here you can find more the full text:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated
I have used this method for a "reads" table (with post_id and user_id) as well as for subscriptions and similar kind of relationships.
The first way is called "hasAndBelongsToMany" [details here].
The second is called "hasMany through" [details here].
The second link relating to "hasMany through" has details and a lengthy explanation about when and why you would want to use it.
Not sure about the specifics of cakephp, but in general defining the relation model explicitly gives you more control over it, for instance if you wanted to do some validation or add callbacks on creation of this relationship.

Categories