You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a common use case when we need to synchronize a one-to-many relationship.
For example when we have a Product model which contains a list of Specification
class Product extends Model {
//...
public function specifications() {
return $this->hasMany(Specification:class);
}
}
class Specification extends Model {
//...
}
and the data in the specifications table
id | product_id | title | value
10 | 42 | old title | old val
20 | 42 | del title | del val
$product = Product::find(42);
// Id of models we wish to keep
$toKeepIds = [10];
// deleting the unneeded elements
$product->specifications()->whereNotIn('id', $toKeepIds)->delete();
// update or create new elements
$product->specifications()->upsert(
$toSave, // Data to be created or updated
['id'] // Unique Id Column Key
);
It will be great to have such a method built-in the framework
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a common use case when we need to synchronize a one-to-many relationship.
For example when we have a Product model which contains a list of Specification
and the data in the
specifications
tableand the specifications to save
currently we can synchronize the date by
It will be great to have such a method built-in the framework
Beta Was this translation helpful? Give feedback.
All reactions