Coder Social home page Coder Social logo

joren.co's People

Contributors

jorenvanhee avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

brieuco

joren.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']);

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.