DEV Community

Jules ADONSI
Jules ADONSI

Posted on

Laravel tip! How to pass an array of data in parameters to an API rest endpoint

Little trick to pass a data table to your method with laravel.

/*
* Url schema
* http://127.0.0.1:8000/api/messages/?message[]=450&message[]=451&message[]=452
*/ 

//Endpoint
Route::delete('messages/{message?}', [MessageController::class, 'delete']);

// Methode delete of MessageController
public function delete(Request $request){
  $messageIds = $request->message;

  Message::delete($messageIds);
}
// The variable $messageIds will be an array containing the ids of the messages 
// to be deleted by the function, when you use dd($messageIds) you can see! 

array:3 [
  0 => "450",
  1 => "451",
  2 => "452"
]

Enter fullscreen mode Exit fullscreen mode

Top comments (0)