Reputation:
I'm following this tutorial which suggests a Resource Controller Laravel API Setup.
Everything is working well in a Resource Controller, I have a fair understanding as to what's going on, but when I get to the update function, I return a successful message but never update the database.
Here it is (verbatim from site):
public function update($id)
{
$url = Url::where('user_id', Auth::user()->id)->find($id);
if ( Request::get('url') )
{
$url->url = Request::get('url');
}
if ( Request::get('description') )
{
$url->description = Request::get('description');
}
$url->save();
return Response::json(array(
'error' => false,
'message' => 'url updated'),
// or 'message' => $url),
200
);
}
From a quick look, can you see why it's just returning what is already in the database? It doesn't even like to change the "updated_at" timestamp Laravel seems to be pretty smart about.
Upvotes: 1
Views: 1942
Reputation:
So the problem ended up being the data format that I was sending with Postman REST Chrome Extension.
Even though I could get away with POST'ing data using the default form-data
encoding, to PUT data, I had to use x-www-form-urlencoded
in the options. I've found a nice resources on SO about this: application/x-www-form-urlencoded or multipart/form-data?
Upvotes: 1
Reputation: 361
I'm not 100% certain but it seems like there's a few problems with your code. I would make a few changes.
public function update($id)
{
$url = Url::find($id);
//Alternatively something like
/*$url = URL::whereUserId(Auth::user()->id)->whereId($id)->first();*/
if ( Request::has('url') )
{
$url->url = Request::get('url');
}
if ( Request::has('description') )
{
$url->description = Request::get('description');
}
$url->save();
return Response::json(array(
'error' => false,
'message' => 'url updated'),
),
200
);
}
Upvotes: 2