laravel - Lumen: update records via json body -
i have laravel lumen api. i'm seeing issue update functionality.
in controller, code updating item is:
public function update(request $request, $id) { $this->validate($request, [ 'name' => 'required', 'description' => 'required', 'completed' => 'required', ]); $todo = todo::find($id); $todo->name = $request->name; $todo->description = $request->description; $todo->completed = $request->completed; $todo->save(); return response()->json(['status' => 'success']); }
i can update todo item using:
http://lumen-todo.app/api/51?name=test&description=test&completed=1
however hoping send parameters in json body, this
put http://lumen-todo.app/api { "id": 1 "name": "test", "description": "test", "completed": 1, }
for adding items, works via json body, don't understand why not work updates. info, 'add item' controller code here:
public function store(request $request) { $this->validate($request, [ 'name' => 'required', 'description' => 'required', 'completed' => 'required' ]); $todo = new todo(); $todo->name = $request->name; $todo->description = $request->description; $todo->completed = $request->completed; $todo->save(); return response()->json(['status' => 'success']); }
instead of doing this:
$todo = new todo(); $todo->name = $request->name; $todo->description = $request->description; $todo->completed = $request->completed; $todo->save();
do, this:
use app\todo; protected $model; public function __construct(todo $model) { $this->model = $model; } $resource = $this->model->find($id); $resource->fill($request); $resource->save();
also, can json_decode()
function change json params array , use validate , save data.
Comments
Post a Comment