L
L
Li2020-08-25 15:54:23
Laravel
Li, 2020-08-25 15:54:23

How to correctly set a checkbox for checkboxes?

The restaurant editing view has checkboxes with all payment methods, you must set checked for those used by the restaurant. How is it customary to solve such a problem in laravel, so as not to make a request in each iteration? Thank you.

Controller:

$restaurant = Restaurant::find($id)->load('payMethods');
$payMethods = PayMethods::all();


Performance:
@foreach($payMethods as $payMethod)
    <div class="custom-control custom-checkbox">
        <input name="pay_method" type="checkbox">{{ $payMethod->name }}</label>
     </div>
 @endforeach

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-08-25
@cubaPro

Something like $restaurant->payMethods->contains($payMethod)
https://laravel.com/docs/7.x/eloquent-collections#...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question