title: Api Response and Json laravel format
body: <p>I'm using Laravel 5.7. and GuzzleHttp 6.0 to get API response 
  from endpoint</p>
  
  <p>I'm passing query data from Blade form to this function. </p>
  
  <pre><code>public static function prhmulti($multisearch, $start ,$end)
  
  {   $city = $multisearch['city'];
  
    $client = new Client([
       'base_uri' =&gt;  'https://avoindata.prh.fi/tr/',
       'query' =&gt; [
      'totalResults' =&gt; 'true',
      'maxResults' =&gt; '1000',
      'registeredOffice'=&gt; $city,
      'companyForm'=&gt;'OY',
      'companyRegistrationFrom'=&gt;$start,
      'companyRegistrationTo'=&gt;$end,
  ],
         'defaults'=&gt;[
         'timeout'  =&gt; 2.0,
         'cookies' =&gt; true,
         'headers'  =&gt; [
           'content-type' =&gt; 'application/json',
           'User-Agent' =&gt;"GuzzleHttp/Laravel-App-5.7, Copyright MikroMike"
         ]]]);
  
  $res = $client-&gt;request('GET','v1');
  $ResData = json_decode($res-&gt;getBody()-&gt;getContents());
  </code></pre>
  
  <p>dd ($ResData) gives all data from API response.</p>
  
  <p>But I am not able to return JSON back to other function </p>
  
  <pre><code>return $this-&gt;multisave($ResData);
  
  
  
  public static function multisave (data $ResData)
  </code></pre>
  
  <p>This will parse JSON and </p>
  
  <pre><code>{
   foreach ($data-&gt;results as $company) {
      $name = $company-&gt;name;
      $Addr = $company-&gt;addresses;
      $businessId = $company-&gt;businessId;
      $companyForm = $company-&gt;companyForm;
      $registrationDate = $company-&gt;registrationDate;
  
    foreach ($company-&gt;addresses as $Addr) {
        $city = $Addr-&gt;city;
        $postcode = $Addr-&gt;postCode;
        $street = $Addr-&gt;street;
     }
   }
  </code></pre>
  
  <p>save data to Mysql.</p>
  
  <pre><code>$NewCompany = new Company();
  
   $NewCompany = Company::updateOrCreate($array,[
   [ 'vat_id', $businessId],
   [ 'name', $name],
   [ 'form',$companyForm],
   [ 'street', $Addr],
   [ 'postcode', $postcode],
   [ 'city', $city],
   [ 'regdate', $registrationDate],
   ]);
  
  }
  </code></pre>
  
  <p>IF Parse part and Save part is inside same function code works ok(save only one company), </p>
  
  <p>but I need to separate them because later on it's easier to maintain.</p>
  
  <p>Error which I am getting to return $ResData </p>
  
  <pre><code>" Using $this when not in object context" 
  </code></pre>
  
  <p>Information is in JSON array.</p>
  
  <p>Also foreach part save ONLY one company ?</p>
  
  <pre><code>  foreach ($data-&gt;results as $company) {
    $name = $company-&gt;name;
    $Addr = $company-&gt;addresses;
    $businessId = $company-&gt;businessId;
    $companyForm = $company-&gt;companyForm;
    $registrationDate = $company-&gt;registrationDate;
  
  foreach ($company-&gt;addresses as $Addr) {
     $city = $Addr-&gt;city;
     $postcode = $Addr-&gt;postCode;
     $street = $Addr-&gt;street;
   }
  </code></pre>
  
  <p>So : 1)  What is best way to create own function for parse JSON
           and other for save data to DB?
       2)  As foreach loop save only one company data, What is 
           best way to fix it?</p>
  
  <p>Thanks MikroMike.</p>