Wednesday, April 17

Tag: Laravel Eloquent

Web Tricks

Laravel Eloquent: API Resources

Introduction When creating API's, we sometimes specifying the data they want back in the various controller actions: public function show(Book $book) { return response()->json([ 'data' => [ 'title' => $book->title, 'description' => $book->description, 'author' => $book->author->name ] ]); } Notice we omitted the attributes created_at and updated_at when formatting the response? Take another scenario where we want to update a book and expect a response back. public function update(Request $request, Book $book) { $book = $book->update($response->all()); return response()->json([ 'data' => [ 'title' => $book->title, 'description...