-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Input::json should not convert object to array #443
Comments
This has nothing to do with the There is no way really to do something about this for everyone. If you really want an object, do a cast or simply override |
I much prefer how it is now. |
Yeah, just do |
The above proposed solution only works for simple objects. For the following nested object:
...the only strategy is to recursively convert the inner-most objects and then construct the parent object. Right now I am sending objects in one end and getting associative arrays out the other which becomes annoying. |
There is nothing that makes StdClass more appropriate to map "javascript objects" than an associative array does in PHP. Arrays are much nicer. Have a geez out there for something to recursively convert to StdClass should you really require it. It seems more consistent to use arrays here than StdClass. On 28/02/2013, at 4:33 PM, aleemb [email protected] wrote:
|
The problem is I'd like to be able to simulate the data that I get when I'm pulling records from a database. With results from the database, I can use the object->property notation, but apparently Laravel is having problems letting me pass data to templates when I convert an array to an object (using the code shown below): class T {
public static function arrayToObject($array){
return json_decode(json_encode($array));
}
} Then, when I do this in my Laravel controller: $data = T::arrayToObject($data);
return View::make('blade-template')->with($data); I get this error: Illegal offset type And when I try this in Laravel controller: $data = T::arrayToObject($data);
return View::make('blade-template',$data); I get this error: array_merge(): Argument #2 is not an array Any suggestions? I'd definitely prefer to use object notation instead of arrays in my templates. Thank you in advance for taking the time to look at this... I appreciate all of your help! |
Not true, this wrecks empty objects: json_encode(json_decode('{}')); // '{}'
json_encode(json_decode('{}', true)); // '[]' I'm sorry to reply to a 4 year old post, but this sort of horrible misconception really ruins the integrity of JSON data and makes my life hell when PHP devs do this with reckless abandon. |
Nice work, good edge case. |
The mainly problem here is you are assuming one or other way works for everyone, limiting the flexibility and providing a tool that we must tweak in order to cover base cases in API concept. {} and [] are valid values and different structures in json format. {}: empty object I think to store them in a Documents DB make sense, and to use a Documents DB on laravel is possible, so... Wont be more flexible to do not force the json_decode with true and allow users to decide by config option or something like that? |
Why this issue is closed? I need to save very a complex json object (wysiwig internal representation) as JSON field in DB. Currently Laravel casts I know that php is dynamic language, but so is JS, and changing object structure without programmer's knowledge because this is convenient is just plainly irresponsible. |
…tion-flow Merging this to test timezones on test & production server
knew that this old post, but i just stumbled in this problem. explanation: |
Perfect. The other solutions around arrays...its cleaner to work with object->property notation than with quotes...and its totally annnoying to move back and forth between array notation and object notation. That way its not possible to deliver clean code / professional work. PHP 8 and Laravel are to some degree object oriented and type save, but than we have non-configurable conversion of objects to arrays in core components? |
When trying to pass around an object,
Input::json()
returns an array instead. When the request payload contains the following JSON object:The expected behavior should be:
Instead each object property must be accessed as an array:
This used to work fine before the
array_get
call added somewhere around cd9fcd0.The text was updated successfully, but these errors were encountered: