mercredi 24 juillet 2019

Laravel validation on array with two fields exists on a database row

i have a input array with each item contain 2 fields

"items" :[
    {
        "product_id" : 23232,
        "product_offer_id" : 3434
    },
    {
        "product_id" : 4545,
        "product_offer_id" : 67676
    }

]

i want to verify if the product_offer_id and product_id exists in products table

current rule

$rules = [
                'items' => 'required',
                'items.*.product_id' => 'required|exists:products,id',
                'items.*.product_offer_id' => 'required|exists:product_offers,id',
                'items.*.quantity' => 'required|numeric',
            ];

with out array i did it like this

        $product_id = \Input::get('product_id');

        $rules = [
            'product_id' => 'required|exists:products,id',

            'product_offer_id' => [
                'required',
                \Illuminate\Validation\Rule::exists('product_offers','id')
                ->where(function ($query) use ($product_id) {
                    $query->where('product_id', $product_id);
                })
            ]
        ];



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire