Hi,
I've been wondering what would be the best way to go about splitting certain logic.
I started with traditional controllers using Eloquent to retrieve and return data directly into the view, e.g.:
$checkins = Checkin::where('user_id', Auth::user()->id)
->with(['location', 'status'])
->paginate(25);
return view('checkin.index', compact('checkins'));
Now with all the fuzz and focus on having a full API layer, I am wondering whether I should move all data interactions to APIs and only return JSON. That would update above code to _(and move the 'query logic' to the API controller)_:
$client = new \GuzzleHttp\Client([
'base_uri' => config('app.api'),
]);
try
{
$response = $client->request('GET', 'api/v1/checkins');
$body = $response->getBody()->getContents();
$checkins = json_decode((string) $body, true);
}
catch (\GuzzleHttp\Exception\ClientException $exception)
{
abort($exception);
}
return view('checkin.index', compact('checkins'));
This allows me to fully separate web and API and have all data logic in one place (API). But on the other hand it feels like making things relatively complex/ slow as I believe it is faster to directly query a data model via Eloquent than it is to do that via an API.
Or obviously there could be a hybrid way, though if I decide to split API from Web, that would mean isolating my Models to a separate repository for both API and Web to refer to.
What would you recommend?
- Stay to Eloquent
- Move everything to APIs
- Use a hybrid model
Thanks.