Comments (6)
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.
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.
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.
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.
Ok, no problem. I should be able to take a look at it this evening.
from laravel-json-api.
@josh-taylor thanks for your input! This has been included in the v0.4.1
release.
from laravel-json-api.
Related Issues (20)
- Unable to create json api resource with attribute name "type" HOT 3
- Self-relation doesn't include model fields HOT 5
- Arguement 3 passed to controller must be an instance of model, string given
- Support for multiple operations in a single request HOT 2
- use Errors Handling in Laravel 8 HOT 1
- Why does json-api trimming spaces in my attributes when updating? HOT 3
- filter slug and underscore HOT 1
- Multiple primary keys - Mysql Error HOT 1
- Sparse fieldsets not working with included relationships HOT 3
- How to attach a middleware to a specific resource relationship? HOT 3
- How I can filter included resources by their fields HOT 8
- Migrate from cloudcreativity/laravel-json-api to laravel-json-api/laravel HOT 15
- How to stop this library from making asterix queries? HOT 1
- Make a request to one endpoint from another controller HOT 5
- Insert on duplicate key upd when inserting a record? HOT 6
- Handle HasMany relationship with extra field. HOT 2
- [5.x] Missing meta HOT 8
- bug: service provider boot throws "No application encryption key has been specified." HOT 1
- Error in tests HOT 1
- getResourceLinks function HOT 3
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.
from laravel-json-api.