title: Laravel Loop through Array Value and Update Users
body: <p>I have an app I am working on. I am supposed to get some data from the external API. I am comparing the email from the database to the email in the response, so as to be able to update the user associated with a particular email. </p>
  
  <blockquote>
    <p>Here is my full code</p>
  </blockquote>
  
  <pre><code>public function checkpaysub(Request $request) {
    $userss = User::all()-&gt;pluck('email');
    //dd($userss);
    foreach($userss as $users) {
      $email = $users;
  
    //$ref= $request-&gt;reference_code;
    $url= 'https://api.paystack.co/customer/';
    $url .= $email;
    //dd($url);
  
    $client = new Client();
    $response = $client-&gt;request('GET', $url, [
      'headers' =&gt; [
        'Authorization' =&gt; 'Bearer '.'##################',
      ],
  
    ]);
  
    $statusCode = $response-&gt;getStatusCode();
    $body = json_decode($response-&gt;getBody()-&gt;getContents());
    $sub_cus = $body-&gt;status;
    $sub_status = $body-&gt;data-&gt;subscriptions[0]-&gt;status;
    $sub_email = $body-&gt;data-&gt;email;
    $user_exist = User::where('email', $sub_email)-&gt;exists();
  
    if(($body-&gt;status == 'true') &amp;&amp; $user_exist &amp;&amp; $sub_status){
       $user = User::where('email', $sub_email)-&gt;first();
       $user-&gt;sub_status = $sub_status;
       $user-&gt;save();
       $users = new UserResource(User::where('email', $sub_email)-&gt;first());
  
       return response()-&gt;json(array(
        'user' =&gt; $users
    ));
  
     }
     elseif (($body-&gt;status == 'true') &amp;&amp; $user_exist) {
      $user = User::where('email', $sub_email)-&gt;first();
      $user-&gt;sub_status = "inactive";
      $user-&gt;save();
      $users = new UserResource(User::where('email', $sub_email)-&gt;first());
      return response()-&gt;json(array(
        'user' =&gt; $users
      ));
      }
    }
  }
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p>This only updates one user. I want to loop through the email and update each user based on the email.</p>
  </blockquote>