Laravel request nested parent/child forms
P粉446800329
P粉446800329 2023-08-08 18:04:08
0
1
484

I am using laravel to create an api. I have created ProductRequest and ProductPriceRequest classes for request validation and they work as expected.

A Product can have multiple ProductPrices.

Now, in order to create a ProductPrice, I need to first create a Product and then create a ProductPrice associated with that Product


;
//Create product POST->http://127.0.0.1:8000/api/v1/products { "name":"product1" } //Create productprice associated with product Post->http://127.0.0.1:8000/api/v1/products/69081258758830188/productprices { "name":"productprice1" }

But I want to enable the creation of ProductPrice when creating a product

//Create product productprice POST->http://127.0.0.1:8000/api/v1/products { "name": "prod2", "productPrices": [ { "name": "pp1" }, { "name": "pp2" } ] }

Since I already have (and need) a ProductPriceRequest, how can I use the ProductPriceRequest class in the ProductRequest to validate the ProductPrice if there is a ProductPrice in the request.

I'm new to laravel (I'm using version 10) and php and didn't find any examples about this situation


< ;/p>

P粉446800329
P粉446800329

reply all (1)
P粉129275658

Personally, in another ProductPriceRequest class, I would not try to reuse the same rules; for ProductRequest, I would just do something similar

'prices.*.name' => ['required', 'min:3'], 'prices.*.id' => ['numeric'],

Or whatever your rules are.

Technically if you really want to use the same rules you could put these duplicate rules in the ProductPrice model and then use them in both FormRequest files, but I'm not sure yet There would be other conflicts, because here, the data is in the form of an array, while in another case (you didn't show elsewhere that you use and require ProductPriceRequest), another structure for validation may be passed

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!