Git Product home page Git Product logo

Comments (6)

lindyhopchris avatar lindyhopchris commented on August 10, 2024

Hi Josh.

Your totally right, this would be best on the controller rather than a model listener. In the current version you'd need to overload the commit method:
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Http/Controllers/EloquentController.php#L239

For example:

protected function commit(Model $model)
{
    $isUpdate = $model->exists;
    $result = parent::commit($model):

    if ($result && $isUpdate) {
        dispatch(new SomeEvent($model));
    }

    return $result;
}

That'll work with the current version (0.4.0).

A slightly better solution might be for me to put in protected methods - didUpdate, didCreate for the those two instances and didCommit that gets called for both create/update. What do you think about that as an approach? It would mean instead of the above you could do:

protected function didUpdate(Model $model)
{
    dispatch(new SomeEvent($model));
}

from laravel-json-api.

josh-taylor avatar josh-taylor commented on August 10, 2024

Yeah, that seems like a good solution to me.

I guess there could also be a beforeUpdate/beforeCreate/beforeCommit too?

Maybe the naming convention could be similar to Eloquent?

protected function updating(Model $model) { // ... }

protected function updated(Model $model) { // ... }

What do you think? I don't mind helping out with a PR for this.

from laravel-json-api.

lindyhopchris avatar lindyhopchris commented on August 10, 2024

I like the idea of using the Eloquent names - I'd normally use willUpdate/didUpdate, but in terms of it being more intuitive for people who don't know my quirks the Eloquent names make sense!

I'm happy for you to submit a PR if you have time. You need to submit against the develop branch. The change should be non-breaking so can be included in the 0.4.1 release I'm preparing (you'll see a number of non-breaking changes on the develop branch at the moment).

from laravel-json-api.

lindyhopchris avatar lindyhopchris commented on August 10, 2024

Oh, if you are doing a PR you don't need to worry about tests. I haven't got this package unit tested at the moment as it's fully integration tested in two production apps. So I'd check that your PR doesn't break those apps before merging.

(I am planning on adding unit tests in the future but at the moment I've got close to 100% test coverage with the integration tests, so to save development time I'm relying on those.)

from laravel-json-api.

josh-taylor avatar josh-taylor commented on August 10, 2024

Ok, no problem. I should be able to take a look at it this evening.

from laravel-json-api.

lindyhopchris avatar lindyhopchris commented on August 10, 2024

@josh-taylor thanks for your input! This has been included in the v0.4.1 release.

from laravel-json-api.

Related Issues (20)

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.