Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add handling of object being passed into old method #41842

Merged
merged 7 commits into from
Apr 6, 2022

Conversation

Drewdan
Copy link
Contributor

@Drewdan Drewdan commented Apr 5, 2022

Hey Guys!

This PR allows users to pass in a Model as the default parameter to the old helper function.

If you had a form to update a username. You might set the value to something like this:

<input type="text" name="name" value="{{ old('name', $user->name) }}">

This will find the old value in the request or, default to the $user->name value which is being pulled from the User model if there is no old value.

This PR changes this to allow the following

<input type="text" name="name" value="{{ old('name', $user) }}">

The method has been updated to check if the default value being passed is an instance of a Model and gets the attribute based on the key

As many developers will follow cruddy conventions, and have their attributes named the same as their input names, so they can mass insert, I believe this would streamline form creation (if only slightly)

I have added a test to cover this behaviour, and I have also added a test to cover setting the default as an array, to ensure no existing behaviour is broken by this change.

An alternate approach I considered would be to add a new helper method, (though, not sure what the name would be, oldAttribute($class, $key) maybe?) , which would do this behaviour independently of the old helper method. I'd be happy to scope this out if this approach would be preferred.

Thanks for looking :)

@Drewdan Drewdan changed the title Add handling of model being passed into old method [9.x] Add handling of model being passed into old method Apr 5, 2022
@Drewdan Drewdan marked this pull request as draft April 6, 2022 11:56
@Drewdan Drewdan marked this pull request as ready for review April 6, 2022 12:17
@Drewdan Drewdan changed the title [9.x] Add handling of model being passed into old method [9.x] Add handling of object being passed into old method Apr 6, 2022
@Drewdan Drewdan requested a review from driesvints April 6, 2022 14:12
@driesvints
Copy link
Member

I personally think we shouldn't do this and this is best handled in userland but let's see what @taylorotwell says.

@taylorotwell
Copy link
Member

taylorotwell commented Apr 6, 2022

@driesvints

There was no coupling before. You can make instanceof checks against classes that don't exist. So instanceof Model would not fail even if database was not pulled in.

CleanShot 2022-04-06 at 09 41 18@2x

@driesvints
Copy link
Member

Ah damn, I always forget about that behavior of instanceof 🤦

@Drewdan
Copy link
Contributor Author

Drewdan commented Apr 6, 2022

I didn't realise that was a behavior of instanceof so, I learnt something new! :)

@taylorotwell
Copy link
Member

taylorotwell commented Apr 6, 2022

@Drewdan I generally kinda like this PR overall - can you revert your recent changes and bring back the instanceof Model check so I can review it again in that form?

@Drewdan
Copy link
Contributor Author

Drewdan commented Apr 6, 2022

@taylorotwell - I shall do, I will post a new commit shortly

@Drewdan
Copy link
Contributor Author

Drewdan commented Apr 6, 2022

@taylorotwell - I have pushed a new commit up.

This one is different from my initial commit in that is just checks to see if $default is an instanceof Model, and if it is, calls getAttribute rather than also checking the array returned by getAttributes contains the key.

The reason for this is I believe getAttribute handles all of this anyway, so I think checking the array contains the key is redundant.

I have also left the array test in just to make sure this does not break any existing behavior

@taylorotwell taylorotwell merged commit ff197bf into laravel:9.x Apr 6, 2022
@taylorotwell
Copy link
Member

Thanks!

@Drewdan
Copy link
Contributor Author

Drewdan commented Apr 6, 2022

Thank you :)

@Drewdan Drewdan deleted the pass-model-into-old-method branch April 6, 2022 19:05
@joshhanley
Copy link
Contributor

joshhanley commented Feb 2, 2023

I know this is an old PR by now, but I've finally had a change to upgrade my Laravel version on one of my apps, and found this caused a breaking change for me. I've fixed up my code, so it's not an issue anymore for me, but I thought I'd make a note 🙂

In my scenario, I'm using the old() function to determine whether to output checked on a checkbox. In my case though, I'm not using the same name for the key as attributes available on the pivot model, I'm more just checking the existence of the pivot table model in the variable.

@if(old('operators.' . $loopIndex . '.isAllocated', $jobAllocation)) checked @endif

Previously, it would return the default variable if no old value was set, as such would be true for my conditional.

But now it's trying to look for the key 'operators.' . $loopIndex . '.isAllocated' on the $jobAllocated pivot model, which doesn't exist, hence ->getAttribute() is returning null. Therefore my checks are always returning false when the default is being used, even if the variable $jobAllocated is populated.

I've updated my code now to just wrap the variable in an isset() and it's all working happily again.

@if(old('operators.' . $loopIndex . '.isAllocated', isset($jobAllocation))) checked @endif

Hope this helps someone else if they are running into a similar problem!

@Drewdan
Copy link
Contributor Author

Drewdan commented Feb 2, 2023

@joshhanley that's interesting, I'd never thought of that sort of usage. Thank you for documenting it here, though. I hope I didn't cause too much of a headache for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants