simple laravel rest api code example

Example 1: 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]); // add $data here
    $gallery->save();

Example 2: encode in laravel api

//In base controller
protected function jsonResponse($data, $code = 200)
{
    return response()->json($data, $code,
        ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'], JSON_UNESCAPED_UNICODE);
}
//In your controller
$this->jsonResponse($data);

Tags:

Php Example