Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.9k views
in Technique[技术] by (71.8m points)

php - Undefined property: Symfony\Component\HttpFoundation\ParameterBag::$product_id"

This is a laravel vue project. I want to update my Cart.
Request sending to controller look like enter image description here

My Controller

public function update(Request $request)
    {
        $carts = Auth::user()->carts;
        foreach ($carts as $cart){
           $cart->delete();
        };

        foreach ($request as $data){
            $newCart = new Cart();
            $newCart->user_id = Auth::id();
            $newCart->product_id = $data->product_id;
            $newCart->variant_id = $data->variant_id;
            $newCart->color_id = $data->color_id;
            $newCart->quantity = $data->quantity;
            $newCart->save();
        }

        return response()->json('Cart Successfully Updated',200);
    }

Now Why I'm facing Undefined property: SymfonyComponentHttpFoundationParameterBag::$product_id" error

question from:https://stackoverflow.com/questions/65857141/undefined-property-symfony-component-httpfoundation-parameterbagproduct-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$.ajax({
  url: "your/route",
  type: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  data: JSON.stringify([{"id":13,"user":1,"product":4},{"id":4,"user":8,"product":4},{"id":6,"user":90,"product":23}]),
  success: function (result) {
    console.log(result);
  }
});

Assuming you are submitting an ajax request with the above settings and data, you can easily go through it in your controller like:

foreach ($request->all() as $data){
    // your code here.
    // You can access each item's data like: $data->id, $data->user etc.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...