title: Laravel - Store JSON response in SQL database (via Guzzle)
body: <p>First post! New to php &amp; Laravel, figured I'd learn by creating a test project. I've been following Laracasts for guidance but run into issues.</p>
  
  <p>What I'm looking to achieve:</p>
  
  <ul>
  <li>Utilise Guzzle to call for an API</li>
  <li>Store response into mySQL database</li>
  <li>Setup a route for this</li>
  <li>Setup a schedule so the controller runs once a day on schedule (I can start a new thread if need be)</li>
  </ul>
  
  <p>I've got a controller setup for the Guzzle &amp; storing data. I've got a database created which works as intended. The route I'm unsure about though on what exactly needs to be done so I'm struggling here on how to actually run the controller and store the data in the database.</p>
  
  <p>I'd appreciate if anyone could review my code if I've done anything wrong, and give some guidance on routes in context of this.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use GuzzleHttp\Client;
  
  class DataController extends Controller
  {
      public function index()
          {
              $client = new Client(['base_uri' =&gt; 'https://api.ratings.food.gov.uk/ratings']);
              $response = $client-&gt;request('GET', [
                  'headers' =&gt; [
                      'x-api-version' =&gt; '2',
                      'Accept'        =&gt; 'application/json'
                  ]
              ]);
              $mydata = json_decode($response-&gt;getBody()-&gt;getContents(), true);
              $object = new Object();
              $object-&gt;ratingId = $mydata-&gt;ratingId;
              $object-&gt;ratingName = $mydata-&gt;ratingName;
              $object-&gt;ratingKey = $mydata-&gt;ratingKey;
              $object-&gt;ratingKeyName = $mydata-&gt;ratingKeyName;
              $object-&gt;schemeTypeId = $mydata-&gt;schemeTypeId;
              $object-&gt;save();
              Requests::insert($object);
          }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>Migration</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateRatingsTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('ratings', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;timestamps();
              $table-&gt;integer('ratingId');
              $table-&gt;string('ratingName');
              $table-&gt;string('ratingKey');
              $table-&gt;string('ratingKeyName');
              $table-&gt;integer('schemeTypeId');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('ratings');
      }
  }
  </code></pre>
  
  <p><strong>API JSON Response Example</strong></p>
  
  <pre><code>
  {
      "ratings": [
          {
              "ratingId": 12,
              "ratingName": "5",
              "ratingKey": "fhrs_5_en-gb",
              "ratingKeyName": "5",
              "schemeTypeId": 1,
              "links": [
                  {
                      "rel": "self",
                      "href": "http://api.ratings.food.gov.uk/ratings/12"
                  }
              ]
          },
          {
              "ratingId": 11,
              "ratingName": "4",
              "ratingKey": "fhrs_4_en-gb",
              "ratingKeyName": "4",
              "schemeTypeId": 1,
              "links": [
                  {
                      "rel": "self",
                      "href": "http://api.ratings.food.gov.uk/ratings/11"
                  }
              ]
          },
          {
              "ratingId": 10,
              "ratingName": "3",
              "ratingKey": "fhrs_3_en-gb",
              "ratingKeyName": "3",
              "schemeTypeId": 1,
              "links": [
                  {
                      "rel": "self",
                      "href": "http://api.ratings.food.gov.uk/ratings/10"
                  }
              ]
          }
  }
  </code></pre>