2

Suppose I have a resourcefull route in Laravel 4.1, like next:

Route::group(['prefix' => 'api',], function()
{
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: Content-Type');
    Route::resource('user','UserController');
});

And I have controller method store() like next:

    public function store()
    {
        $user = new User;
        $name = Input::get('name');
        $user->login_name = implode('.', explode(' ', strtolower($name)));
        $user->name = $name;
        $user->email = Input::get('email');
        $user->password = Hash::make('1');
//      $user->save();
        return Response::json([
                'error' => false,
                'user' => $user->toArray()
                ], 200);

    }

I am making post request either from backbone, or just from rest api test tool: the request is following:

{"phone":"123123123","description":"","name":"test","email":"[email protected]"}

The problem is that when I look at the response I see that request was empty :

{"error":false,"user":{"login_name":"","name":null,"email":null}}

What can be the problem?

12
  • is $user->save() saving properly to DB? Commented Dec 23, 2013 at 11:20
  • no, gives the error that field "name" is empty Commented Dec 23, 2013 at 11:24
  • what HTTP method are you using? GET, POST, PUT? Commented Dec 23, 2013 at 13:47
  • POST of course, and If I read php://input then there is 2 json strings(same) Commented Dec 23, 2013 at 13:50
  • what do you get when you try dd($name) in the store() method Commented Dec 23, 2013 at 13:52

1 Answer 1

3

Set the following header in your request

content-type:application/json

or alternatively you can set it in the "App::before" filter

App::before(function($request)
{
    $request->headers->set('content-type','application/json');
});
Sign up to request clarification or add additional context in comments.

Comments

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.