September 2017
Intermediate to advanced
244 pages
6h 44m
English
The delete operation will be served by destroy($id). Again, it depends on ID coming from an API user, so we need to place a similar check as we placed for update() and show(). Here is what it will look like:
public function destroy($id){ $post = $this->post->find($id); if(!$post) { abort(404); } $post->delete(); return ['message' => 'deleted successfully', 'post_id' => $id];}
With that, our PostController will look like this:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Http\Response;use Illuminate\Http\JsonResponse;class PostController extends Controller{ public function __construct(\App\Post $post) { $this->post = $post; } /** * Display a listing of the resource. *
Read now
Unlock full access