In my mind, this is a perfect use case for Laravel's queue system. My approach would be to validate input and create the user in this controller, then dispatch a job (something like CreateEconomicCustomer, perhaps) to the queue—either directly from the controller, or via an event listener.
The job would then take over the responsibility of making the API call to create the remote record and updating the user record with the economic_customerNumber. I suspect that field is not currently in your $fillable array, so it is not being mass-assigned. For example:
<?php
namespace App\Jobs;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class CreateEconomicCustomer implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
protected $data;
public function __construct(User $user, $data)
{
$this->user = $user;
$this->data = $data;
}
public function handle()
{
// Delegate actual API logic and update user...
$this->user->sendToApi($this->data);
}
}
I also suggest looking into using Guzzle instead of directly calling cURL. It will give you much better control of your HTTP requests.
Finally, if you are on a server where running a queue worker is too difficult, you could always trigger artisan commands via cron/Laravel scheduled tasks. For example, find all user records where the economic user field is null, then run the API call on each one of those, updating the user record in the process.