Read

Fetch an existing model

A controller with any of these Traits can fetch a single existing model. The traits provide the show($id) method which a route can connect to for the model.

<?php

// ...

Route::get('task/{id}', 'TaskController@show');

📘

Tip

If the model with the id does not exist, method notFoundError() is called, which by default returns a 404 error message.

Before the model is returned to the client ...

To catch the retrieved model before it is sent to the client, override method beforeShowResponse(&$data)

<?php
// ...

public function beforeShowResponse (Illuminate\Database\Eloquent\Model &$model) {
  // Do something on/with the $model
}

📘

Tip

If method beforeShowResponse($data) returns anything other than null, the show process is terminated and the returned value is sent to the client instead.


Next