0

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 rules:

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

Without 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);
        })
     ]
];

1 Answer 1

1

You will need to run your input array which contains your items against the rules that you have written using the Laravel class Validator.

Validator::make($yourInputArray, $rules)->validate();

Sign up to request clarification or add additional context in comments.

Comments

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.