Reputation: 464
I am new to laravel and also creating restful APIs.
I want to send array
of product_id
and product_amount
. This array can have multiple products entries in it, array{ product_id: 1 product_amount: 1000 , product_id: 2 product_amount: 5000}
like this. I want to send this associative array to controller via API and in controller I want to access this array with its fields.
I have gone through some online solutions but that not solving my problem. Like for post array by postman:
$ass_array = array(
“product_id” => “1”,
“product_amount” => “1000”
);
we have to mention
key: array[product_id] , value: 1
key: array[product_amount] , value: 1000
In this case if I add another entry its not showing on postman. how can I post multiple entries through associative array to controller?
Also I have checked that we can access array by dot operator
in controller. But I don't know how I can do this in my case?
Please guide. thanks in advance
Upvotes: 1
Views: 5941
Reputation: 2495
So first you need to create a new controller by using the php artisan make:controller
command. In my case I call it ArrayApiController
(Im sure you will find a better name).
So my command looks like this:
php artisan make:controller ArrayApiController
Now you have a new controller to handle your incomming request. After this we have to create a new route to redirect requests to this new controller.
For this we add a new entry into the api.php
file under routes/api.php
. I suggest to add api related routes in the api.php
to keep it clean and organized.
So add this line into the routes/api.php
file:
Route::post('array', 'ArrayApiController@post');
You can change the ::post
to your prefered http method and the url (array
in my case) to whatever you want (Here are all available http methods).
So requests to /api/array
will now be redirected to the post
function in the ArrayApiController
we've created earlier.
So now create a new function called post
in the new controller (ArrayApiController
in my case):
public function post(Request $request)
{
//
}
You can now get submitted data by using $request->get(NAME_OF_THE_DATA)
. In the next code snippet you can see that I took products
as name of the products variables I submit with postman.
public function post(Request $request)
{
// get all products
$products = $request->get('products');
}
Now you are able to loop over your products and do whatever you want (In my case just expose it but you could also write those variables in the DB):
public function post(Request $request)
{
// get all products
$products = $request->get('products');
// loop through all products
foreach ($products as $product) {
echo $product['product_id'];
echo $product['product_amount'];
}
}
Now go to postman and create a new request. Change the http method to your entered method in the api.php
file (In my case I've added Route::post(..
to the api.php
file so I have to create a POST
request). Then add a new header called Content-Type
and set it to application-json
. As body you can now send your array as json:
{
// You can call the variable how you want
"products": [
{
"product_id": 1,
"product_ammount": 500
},
{
"product_id": 2,
"product_ammount": 1500
}
]
}
Make sure that the content type of your body is json.
Instead of products
you could take what you want but it has tobe the same as in the ArrayApiController
in the $request->get()
statement stated.
If you want to add authentication to the api or more informations about it here is a good link -> Build RESTful API In Laravel 5.8 Example.
Now how you need to make the postman request:
Upvotes: 4