how to write api in laravel 8 code example
Example 1: laravel api response trait
trait RespondsWithHttpStatus
{
protected function success($message, $data = [], $status = 200)
{
return response([
'success' => true,
'data' => $data,
'message' => $message,
], $status);
}
protected function failure($message, $status = 422)
{
return response([
'success' => false,
'message' => $message,
], $status);
}
}
Example 2: how to save data from api to laravel
$res = $gallery->request('GET','https://www.instagram.com/explore/tags/MIZONEDanceBattle/?__a=1');
$data = json_decode($res->getBody()->getContents(), true);
$gallery = Gallery::firtsOrCreate([
'shortcode',
'thumbnail_src',
], $data]);
$gallery->save();