0

i have array from post request

 #parameters: array:6 [▼
  "_token" => "aXtlqdZWcz5lbLyZGn88PJB5oADF9oZz6k2c7PwW"
  "spg" => "test"
  "nama" => "user"
  "hp" => "12345678"
  "alamat" => "surabaya"
  "orderProducts" => array:1 [▼
    0 => array:2 [▼
      "product_id" => "1"
      "quantity" => "1"

how can i validate orderProduct->product_id = "required" and orderProduct->quantity = "min:1"

and this is my $request->validation

$request->validate([
        'spg' => 'required|max:30',
        'nama' => 'required|max:30',
        'hp' => 'required|numeric|digits_between:8,15',
        'alamat' => 'required|max:255',
        'orderProducts.product_id' => 'required',
        'orderProducts.quantity' => 'required|min:1'
    ]);

very grateful if someone help to solve my problem. thanks

5
  • what did you tried so far? update that code also Commented May 1, 2021 at 9:22
  • okay I have included the validation code Commented May 1, 2021 at 9:27
  • just want validate request data before store to database Commented May 1, 2021 at 9:29
  • so you are passing array of objects in OrderProducts right? Commented May 1, 2021 at 9:29
  • yaaaa that's right Commented May 1, 2021 at 9:31

1 Answer 1

5
$request->validate([
    'spg' => 'required|max:30',
    'nama' => 'required|max:30',
    'hp' => 'required|numeric|digits_between:8,15',
    'alamat' => 'required|max:255',
    'orderProducts.*.product_id' => 'required',
    'orderProducts.*.quantity' => 'required|min:1'
])

You can Refer Here

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

1 Comment

@Reynaldo 'orderProducts.*.product_id' => 'required', the OrderProducts is the key and * is the index

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.