madsynn's avatar

Help understanding validation

Can someone please help me with my understanding of validation. I have tried to find the answer for this question but have not been able to as of yet and I am hoping all of you might have my answer.

Lets say i have two models with two tables.

  1. Product::model

  2. Price::model

  3. products::table

  4. prices::table

I have fields from both that are used in my crud for products in my eCommerce build. My forms run through my Product involving the following:

PRODUCT FIELDS: name manufacturer details description

PRICE FIELDS: price model sku upc quantity alt_details

My question is do i put the validation to use in my ProductController like this:

 $this->validate($request, [
            'name' => 'required',
            'manufacturer' => 'required',
            'price' => 'required',
            'details' => 'required',
            'quantity' => 'required'
        ]);

or do i need to split them up and put them in a combined request like CreateProductRequest or put them in their corresponding models?

Does the flow of data make then look like one combined model / controller determined by the relationships or are they separate and need to be addressed that way.

Any help understanding this would be helpful and thank you in advance.

0 likes
4 replies
abhishek009's avatar

No need to put in model. You can put it in the controller. I mean where you post your data.

Snapey's avatar

model and controller does not need to be a 1:1 relationship IMO

I would though create a form request and do the validation there.

The primary entity is the product, the price is just an attribute of product, so I would probably persist through the product controller as you have said

1 like
madsynn's avatar

@Snapey Thank you

But to make sure i am understanding what you are saying is to use the productcontroller to validate all of the product data?

this would as an example include the product, pricing, album, variations, options, and anything else correct.

So i would validate through the productcontroller but in the models i would clarify the fillable and casts for each correct?

I am testing this out and have a basic implementation so far but i am having issues using the pricing table data maybe you can take a look and see if you can see my error. https://laracasts.com/discuss/channels/laravel/relationship-not-returning-fields

Please or to participate in this conversation.