0

i have a array data like this below. i want to save some of it's data not all into mysql table. when i dd($request->all) data in my controller function it looks like this.

array:1 [
  "{"cart":" => array:1 [
    "{"product":{"id":1,"category":1,"product_name":"Brinjal 500g","product_image":"1585409454.jpeg","product_price":232,"unit":"500g","product_description":null,"stock":"In Stock","product_status":1,"show":1,"delievery_date":null,"vendor":null,"quantity":18,"item_type":"feature_product","created_at":"2020-03-28T15:30:54.000000Z","updated_at":"2020-03-28T15:30:54.000000Z","deleted_at":null,"categories":{"id":1,"category_name":"Fresh Fruits","category_image":null,"category_description":null,"category_status":1,"show":0,"created_at":"2020-03-28T15:30:12.000000Z","updated_at":"2020-03-28T15:30:12.000000Z","deleted_at":null}},"quantity":1}" => null
  ]
]

i want to add id and total quantity shows last object into a table. table rows looks like this.

$table->increments('id');
$table->integer('order_id');
$table->integer('product_id')->unsigned();
$table->integer('total_quantity');
$table->timestamps();
$table->softDeletes();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

how can i write a store function using this data in laravel

result data looks like this

Array
(
    [{"cart":] => Array
        (
            [{"product":{"id":1,"category":1,"product_name":"Brinjal 500g","product_image":"1585409454.jpeg","product_price":232,"unit":"500g","product_description":null,"stock":"In Stock","product_status":1,"show":1,"delievery_date":null,"vendor":null,"quantity":18,"item_type":"feature_product","created_at":"2020-03-28T15:30:54.000000Z","updated_at":"2020-03-28T15:30:54.000000Z","deleted_at":null,"categories":{"id":1,"category_name":"Fresh Fruits","category_image":null,"category_description":null,"category_status":1,"show":0,"created_at":"2020-03-28T15:30:12.000000Z","updated_at":"2020-03-28T15:30:12.000000Z","deleted_at":null}},"quantity":1}] => 
        )

)

this is my latest update

$data = [];
foreach ($request->all() as $key => $value) {
   $data[$key]['product_id'] = $value['product']['id'];
   $data[$key]['quantity'] = $value['quantity'];
   dd($value);
}
3
  • 1
    can you echo '<pre>'; print_r($request->all()); Commented Mar 29, 2020 at 5:55
  • can you (echo '<pre>'; print_r($request->all());) we did not understand your data why id field comes 2 times with same value ? Commented Mar 29, 2020 at 6:08
  • @DilipHirapara i updated my question with your request in last columns Commented Mar 29, 2020 at 6:36

1 Answer 1

1

Loop through your data coming and make array as below i have shown.

Try this one by creating the array of the data and insert array to the database as bulk data:

// this will decode your json data that you're getting in the post data
$postdata = json_decode($request->all(), true);

$data = [];
foreach ($postdata as $key => $value) {        
    $data[$key]['product_id'] = $value['product']['id'];
    $data[$key]['quantity'] = $value['quantity'];
}

// $data now will have data as following structure

$data = [[
    'product_id' => 2,
    'total_quantity' => 7   

],
[
    'product_id' => 2,
    'total_quantity' => 7   

]];

$order = Order::create($data);
Sign up to request clarification or add additional context in comments.

8 Comments

i tried this but shows me this error . (json_decode() expects parameter 1 to be string, array given)
@kalana93 so in that you are getting the array in the post so you don't need to include $postdata = json_decode($request->all(), true); this line in your code.
i removed that line and edit foreach like this.foreach ($request->all() as $key => $value) . then it shows this error . Undefined index: product
@kalana93 can you do dd($value); inside the foreach loop and show me output.
i do that. but still shows me "Undefined index: product" error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.