3

I m newbee in laravel. I am sending such type of response through Postman. and in Laravel controller I m getting this response in $request.

[{
    "id": 40,
    "cname": "Ramesh",
    "constraint_value": "",
    "r_id": "6",
    "rtype_id": null,
    "deleted_at": null,
    "created_at": null,
    "updated_at": null,
    "input": "111",
    "input2": "111"
}, {
    "id": 45,
    "cname": "Suresh",
    "constraint_value": "",
    "r_id": "6",
    "rtype_id": null,
    "deleted_at": null,
    "created_at": null,
    "updated_at": null,
    "input": "222",
    "input2": "222"
}, {
    "id": 49,
    "cname": "Raj",
    "constraint_value": "",
    "r_id": "6",
    "rtype_id": null,
    "deleted_at": null,
    "created_at": null,
    "updated_at": null,
    "input": "333",
    "input2": "333"
}]

I am facing difficulty in

  1. How to count the total no of objects in array which I am receiving in $request. I had used count($request) or sizeOf($request) but its returning only 1 although there are 3 arrays. Help me in this.

  2. How to save the data in database through such arrays.I want to save the values of input and input2.

3
  • first of all are you sending these data in JSON format to the controller ?? If yes then you need to decode that into array first... and count($request['data']) will work if you send your data using a data variable so that you can get from request variable. request contains everything that will be transferred to an controller method so you need to extract your data part only Commented Jan 2, 2017 at 7:30
  • 2
    please post that how you get $request variable, is that a instance of Request ??? if yes then $request will not just contain your data, it will contain everything needed in Laravel framework. thats the reason the count you get is one. and if $request is not the instance of Request then just simply decode it as mentioned in following answer by @alexey Commented Jan 2, 2017 at 7:35
  • yes its instance of Request.whats the solution to use it without instance Commented Jan 2, 2017 at 8:18

5 Answers 5

1

Probably late, but may help someone in the future. First of all. You need to iterate over the array of objects like so

foreach($request->all() as $key => $value){
    $model = new Model();
    $model->attribute1 = $value['attribute1'];
    $model->attribute2 = $value['attribute1'];
    $model->attributeN = $value['attributeN']; // where N is infinite # of attr
    $model->save();
}

So what is happening is that each time the loop iterates, it create a new object of your Model type inside the loop and assigns the objects attributes from the array key values and persists in DB and repeats until all the objects in the array are finished. The

$model = new Model();

is necessary so that each time loop iterates It creates a new object and persists. The loop may save only one object if u do not create it each time the N iteration occurs. This may happen if u are creating the model object in the constructor where it is instantiated once when the constructor is run.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to convert it to an array:

$data = json_decode($request, true);
$count = count($data); // Count number of elements.
Model::insert($data); // Insert all elements into DB.

Don't forget to validate any input first and add all fields except timestamps to a $fillable array.

4 Comments

while I returning $count = count($data), it returns 0. and If I returning $data = json_decode($request, true); it returning blank page in postman.
would you please mention how you get $request or what is $request. it must be the array and inside that there must be your JSON data
Post result of dd($request); please.
#json: ParameterBag {#32 #parameters: array:3 [ 0 => array:10 [ "id" => 40 "cname" => "Ramesh" "constraint_value" => "" "r_id" => "6" "rtype_id" => null "deleted_at" => null "created_at" => null "updated_at" => null "input" => "111" "input2" => "111" ] and similarly other 2 objects ] } #convertedFiles: null #userResolver: Closure {#119 class: "Illuminate\Auth\AuthServiceProvider" and all laravel framework data...
0

You can try this..

$data = [{},{}]; // your response
$count = 0; 

foreach($data as $obj)
{
  Model::create($obj); //save the model
  $count ++;
}

echo $count // your count

Comments

0

its quit simple

$request ='[ {"id":40,"cname":"Ramesh","constraint_value":"","r_id":"6","rtype_id":null,"deleted_at":null,"created_at":null,"updated_at":null,"input":"111","input2":"111"}, {"id":45,"cname":"Suresh","constraint_value":"","r_id":"6","rtype_id":null,"deleted_at":null,"created_at":null,"updated_at":null,"input":"222","input2":"222"}, {"id":49,"cname":"Raj","constraint_value":"","r_id":"6","rtype_id":null,"deleted_at":null,"created_at":null,"updated_at":null,"input":"333","input2":"333"}]';
$request = (array)json_decode($request, true);

now use them as you usually use .

3 Comments

I used this method, if I copy paste your this provided code, then its working but if returns only $request = (array)json_decode($request, true); where $request is my request which i post through postman, then it gives output as [ ]
hi first of all (array) is not require hare if i use json_decode($request, true); (sorry for that ) and if you assigned $request = which you post through postman then can you prove the request which you post through postman and gives output as [ ] then i can check the issue ...
it gives blank page in postman for '$request = json_decode($request, true); return $request;'
0

I got the solution. I was making mistake in sending the data from angularjs.I was directly sending the POST request without creating its object like var data = { 'data':my_request} .That is why request was reeving with instance in laravel. But, as I start sending data by creating its object its working. This was the solution for my first problem. Help me how to store that received data in database.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.