[5.1] Example of recently inserted state tracking#9746
[5.1] Example of recently inserted state tracking#9746taylorotwell merged 2 commits intolaravel:5.1from atrauzzi:add-inserted-state
Conversation
Fairly trivial, but I think this could be helpful in many scenarios. Closes #9745.
|
That would be very useful |
|
And of course, anyone is open to suggest a better name for the variable ;) |
There was a problem hiding this comment.
Minor indentation issue here
|
I like the idea. |
|
What were the use cases for this? |
|
If I understand correctly, this would help for places we don't want to hook some function persist(Podcast $podcast, array $attrs) {
$podcast = $podcast ?: new Podcast;
$podcast->forceFill(array_only($attrs, [ 'title', 'url' ]));
$podcast->save();
if ($podcast->recent) {
// do something with $podcast that required it
// already being saved to the db, but only when
// it was just created.
}
}Setting a flag up front is currently necessary. function persist(Podcast $podcast, array $attrs) {
$podcast = $podcast ?: new Podcast;
$creating = ! $podcast->exists; // Need to set a flag here; this PR makes this unecessary
$podcast->forceFill(array_only($attrs, [ 'title', 'url' ]));
$podcast->save();
if ($creating) {
// ...
}
} |
|
Without functionality along these lines, repetitive blocks using In our case, we need it to respond with HTTP 200 or 201. So with this feature, we could do things like this: However, producing the same state without having Eloquent accumulate it (similar to Which is not ideal as the state has to be inferred manually at the time the store/update functionality is used. While it produces the same end result, As I mentioned earlier, if you want :) |
|
I think the variable could be a bit more self-explanatory. What about |
|
@lukasgeiter - I'd like to see if there is a different name that is more specific, although it might be nice to keep it as short as possible. Let's wait for as many suggestions as we can get :) |
|
This feature would be really useful to me too. 👍 |
|
My use case is working out if a user has signed up. |
|
What about |
|
Isn't |
No, not for properties. I tried it out too just to be sure. |
|
Then in that case I'd prefer |
|
👍 for |
|
Yeh. |
|
@Anahkiasen Yeah, I was thinking it might be reserved, too, but double checked before posting. The only issue that crossed my mind was that to someone used to Ruby, it might kinda look like it's creating a new instance, but that's kind of a stretch. |
|
I would have gone even further - if ($model->exists) {
...
}
if ($model->isNew) {
...
} |
|
I think it is bad to add more public properties to the model, it can make conflicts with the dynamic properties, for example if i have a column 'new' or 'recent' inside a table. It should be a protected property that is accessed by a public method. |
|
Yeah, it would be pretty standard to have a protected property |
|
@GrahamCampbell @taylorotwell -- Let me know what you guys would prefer to see and I'll amend my MR. |
|
@atrauzzi It's totally up to @taylorotwell. :) |
|
For sure, @taylorotwell - Whatcha think? :) |
|
Renamed it to |
|
👍 Awesome! |
|
I'm not sure to write this here. See my gist here for an explanation: https://gist.github.com/hauthorn/fb1494c3ba5238283acc This didn't fail before. Now it does. Do I use the "assertViewHas" in some inappropriate way, or is this an unintended consequence of the change? |
Fairly trivial, but I think this could be helpful in many scenarios.
Closes #9745.