Answer the question
In order to leave comments, you need to log in
How to display info at the end of the controller?
Making an API for the front
Created a trait so that all outputs have the same format:
namespace App\Traits;
trait Output
{
protected $success = true;
protected $statusCode = 200;
protected $errors = [];
protected $data = [];
public function setData(array $data = [])
{
$this->data = array_merge($this->data, $data);
return $this;
}
...
public function renderOutput()
{
return response()->json([
'success' => $this->success,
'code' => $this->statusCode,
'errors' => $this->errors,
'data' => $this->data
], $this->statusCode);
}
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests, Output;
}
$this->setData(['user' => $user]);
return $this->renderOutput();
Answer the question
In order to leave comments, you need to log in
As an example that immediately comes to mind, you can use __destruct()
It is always executed at the end of the controller, and can output the necessary information
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question