joren.co's People
Forkers
brieucojoren.co's Issues
Create, update or delete hasMany relationships in Laravel
hey! Your code is almost perfect which i am so grateful for. It taught me a lot.
I have a suggestion, when checking identifiers for new records, you should also check that the given id actually belongs to the parent, if not substitute null.
This will prevent updating/overwriting of record that does't belong to the parent - in case whem someone (intentionally or not) sends an incorrect ID of updated object.
something like this works for me:
public function __invoke()
{
$this->keyName = $this->query->getRelated()->getKeyName();
$this->existingModelIds = $this->query->pluck($this->keyName)->toArray();
...
protected function upsertRecords()
...
$keyName = $this->keyName;
$existingModelIds = $this->existingModelIds;
$values = $this->records->map(function ($record) use ($keyName, $existingModelIds) {
...
// Set $record['id'] to null when missing or when indicated id doesn't belong to parent
$record[$recordKeyName] = array_key_exists($recordKeyName, $record) // $record array has id
&& in_array($record[$recordKeyName], $existingModelIds) // specific id is related with parent
? $record[$recordKeyName]
: null;
...
In addition, your code can also easily be used to create/delete/update morphMany relationships
protected function upsertRecords()
{
...
// Set $record['memberable_id'] to parent key.
$record[$this->query->getForeignKeyName()] = $this->query->getParentKey();
// Set $record['...able_type'] to morphClassName
$record[$this->query->getMorphType()] = $this->query->getMorphClass();
...
what iam currently testing ๐
Create, update or delete hasMany relationships in Laravel
Hello,
Thanks for your post,
But i think its not working with created_at and updated_at values from default Laravel Migrations
Insert value list does not match column list: 1136 Column count doesn't match value count at row 2 (SQL: insert into user_contacts
(created_at
, email
, id
, name
, order
, phone
, service
, updated_at
, user_id
) values (?, [email protected], 2, dazda, ?, +33 6 666 666, adzadaz, ?, 20), ([email protected], ?, dazdazdadzad, ?, +33 6666 6666, dazdada, 20) on duplicate key update id
= values(id
), order
= values(order
), name
= values(name
), user_id
= values(user_id
), phone
= values(phone
), email
= values(email
), service
= values(service
), created_at
= values(created_at
), updated_at
= values(updated_at
))
Detected N+1 Query
Hello, the code really helped me and works well. Thank you for the code. However, there is a problem, it re-runs the query as many records as there are in the related table and logs
[2023-09-15 18:30:14] local.INFO: Detected N+1 Query
[2023-09-15 18:30:14] local.INFO: Model: App\Models\Order
Relation: App\Models\OrderDetail
Num-Called: 2
Call-Stack:
It prints this code. How can we prevent this problem?
The way I run the code:
public function update(OrderRequest $request, Order $order)
{
$validated = $request->validated();
$order->update($validated['order']);
$order->orderDetails()->createUpdateOrDelete($validated['orderDetails']);
}
Create, update or delete hasMany relationships in Laravel
This is a very nice piece of code. I'm wondering if it would be possible to do the same thing for morphMany relationship? Any thoughts on how to go about that?
Thanks, Dan
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.