Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FabienArr's avatar

Sanctum API and many records in one request

Hello,

I use Laravel which is synchronize to an local application. I use the Sanctum Laravel API to send or retrieve records from this application. The problem is that I have more and more records and currently it takes many time because I have a request for each record. Is it possible to send or retrieve a list of record in a same request with this API ?

Thanks for your help.

0 likes
10 replies
Glukinho's avatar

Sanctum is an authentication system for Laravel, it doesn't deal with what you app actually does, with it's logic. So it is not about the topic at all.

If your external application can give/send multiple records at once, then yes, you can tune Laravel to fetch them all in one request.

If not - then you have to handle records one by one.

Provide some code of this integration, maybe we could give you some ideas.

FabienArr's avatar

Hello,

Yes Sanctum is an authentication system, with my external app I can do what i want, create XML or JSON collection...

I have a controller API for each model and a resource too. I have things like this for the moment :

I will take a look to your link. But how I call the API with a JSON collection and after doing an update or store int he API model ?

Regards

Glukinho's avatar

Extract your create logic from controller to some service (call it App\Services\CepageService).

class CepageService
{
	public function create($id, $libelle, $abrege): Cepage
	{
		return Cepage::create($input);
	}


	public function createMany(array $rows): \Illuminate\Database\Eloquent\Collection
	{
		$collection = new \Illuminate\Database\Eloquent\Collection;

		foreach($rows as $row) {
			$created = $this->create(...$row);

			$collection->add($created);
		}
		

		return $collection;
	}
}

Leave validation and response generation to controller:

class CepageController extends BaseController
{
	public function __construct(private CepageService $cepageService)
	{ }


	public function storeOne(Request $request)
	{
		$validated = $request->validate([ /* rules for one input row here */]);
		
		$cepage = $this->cepageService->create(...$validated);

		return new CepageResource($cepage);
	}


	public function storeMany(Request $request)
	{
		$validated = $request->validate([ /* rules for multiple input rows here */]);

		return $this->cepageService->createMany($validated)->toResourceCollection();
	}
Glukinho's avatar

If you want to avoid many INSERTs to database, you can insert many rows in one query by Cepage::insert($array_of_rows). But this method does not set timestamps on created models.

1 like
FabienArr's avatar

Hello,

Thanks for the code and your help. Is possible to do insert and update at the same time ? Or I need to separate insert and update ?

Regards

Glukinho's avatar

@FabienArr sorry, I don't get where/when exactly you want to do insert+update. Please describe more.

FabienArr's avatar

In my code, I just put the code of the function store to create a record. But I have in my Controller a function to show, update or destroy. For the moment, I check from desktop application if the record exists, if yes I send an update to the API, if not I send a create request to the API. It's the same when I need to delete a record, I send a request to show the record, if i get it i send the request to delete it, if he doesn't exists I do noting. The problem is that all of this take time and it does many request, the goal of this operation is too reduce the number of request sending many resquest in one call to the API.

Glukinho's avatar

@FabienArr deleting doesn't require preliminary checking for model existence, just obtain a model by id or with some query, and do->delete() on it:

Cepage::where('name', 'some name')->firstOrFail()->delete();

If a model exists, it will be deleted. If not, an exception will be thrown from firstOrFail() part.

Regarding update/create, there is a method updateOrCreate(), it seems to do what you want, see here: https://laravel.com/docs/12.x/eloquent#updates

Please or to participate in this conversation.