1

I have read and worked with the other posts about this and it appears the version of Laravel 4 I just downloaded has more changes made to the way the JSON input is handled by a controller.

$input = Input::json()->all(); gives me errors as if I am referring to something that does not exist when I request some part of the payload after doing a PUT request. And without ->all(); I get a symfony error. Does anyone know how to get good JSON from backbone in Laravel 4's latest version?

Currently, I am doing the long way around to get my data, ie:

    $input_title = Input::get('title');
        $input_completed = Input::get('completed');

        $task = Task::find($id);
        $task->title = $input_title;
        $task->completed = $input_completed;
        $task->save();

Yes, I am doing the tutorial on tutsplus to learn laravel/backbone, so a little noob patience is apreciated.

The error I get when using Input::get(); is: {"error":{"type":"UnexpectedValueException","message":"The Response content must be a string or object implementing __toString(), \"array\" given.","file":"/Users/brentlawson23/Sites/laravel4App/bootstrap/compiled.php","line":16858}}

I really want to get the Laravel-specific answer instead of using straight php to stringify the payload.

I get same error using just Input::json();

For the current beta of Laravel 4, Input::json(); is not getting a stringified version of the request payload that can be used to create a new row in a table, nor does Input::json()->all(); (hoping to play nice with the ParameterBag from symfony). I have tried json_encode among other hacks and basically every step of the way in this tut, I hit some brick wall. Anyone have a suggestion based on what I have presented here?

Today I got this when simply trying to echo the result of $input = Input::json(); : {"error":{"type":"ErrorException","message":"Catchable Fatal Error: Object of class Symfony\Component\HttpFoundation\ParameterBag could not be converted to string in /Users/brentlawson23/Sites/laravel4App/app/controllers/TasksController.php line 45","file":"/Users/brentlawson23/Sites/laravel4App/app/controllers/TasksController.php","line":45}}

Yes, I have studied the Symfony API.

6
  • Can you just use Input::get() (no parameter) ? Commented Apr 29, 2013 at 18:03
  • so not working actually, simple and intuitive and I appreciate it, but it is giving same error Commented Apr 29, 2013 at 20:22
  • Could you include the error in your question, it may help someone to reproduce/debug. Commented Apr 29, 2013 at 20:57
  • Have you looked at the Symfony ParameterBag API Commented May 1, 2013 at 10:17
  • Yeah and the options seem not to include a method (other than all()) for dealing with more than one key->value pair - I am getting a string with json_encode($input) tha looks like it should work but to no avail so far Commented May 1, 2013 at 18:42

2 Answers 2

1

I had a similar problem. Input from Backbone is converted to array in Laravel. On tutsplus, Jeffrey Way is using object. So I was trying to do this (like in tutorial):

return $input->title // using object,but got an error.

If I change that line to:

return $input["title"] // everything works fine with array.
Sign up to request clarification or add additional context in comments.

Comments

1

I'm also working through the Backbone tutorial on tuts+. If I'm right in assuming are you stuck on the Creating New Contacts section? Below is how I got it to work for me, in ContactController.php:

public function store()
{
    $input = Input::all();
    Contact::create(array(
        'first_name' => $input['first_name'],
        'last_name' => $input['last_name'],
        'email_address' => $input['email_address'],
        'description' => $input['description']
    ));
}

And then also needed to update app/models/Contact.php with the below:

class Contact extends Eloquent {
    protected $fillable = array('first_name', 'last_name', 'email_address', 'description');

}

That should get it working for you and insert the contact into the database. If I've misread let me know and I can have another look.

Cheers,

Sean

2 Comments

for me Input::all() is empty!! but Input::getContent() has a string of JSON data.
@Sean yes! your solution worked very well. I'm also working on tuts+ contacts app. Don't forget to add 'public $timestamps = false' in the 'Contact' model if you are working on this same app. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.