0

I have view code that get values from loop and each value have a checkbox

@foreach($materials as $material)

    <div class="card card-pricing bg-gradient-success border-0 text-center mb-4 mr-3 mx-auto col-lg-4">
        <a href="#" id="{{$material->price}}">
            <div class="card-header bg-transparent">
                <h4 class="text-uppercase ls-1 text-white py-3 mb-0">{{$material->name}}</h4>
                <img src="/zoby/uploads/materials/{{$material->image}}" height="100" class="rounded-circle">
            </div>

            <div class="card-body">
                <div class="display-4 text-white mb-2">{{$material->price}} زومن </div>
                <span class=" text-white"> در ازای هر {{$material->unit}}</span>
            </div>

            <div class="custom-control custom-checkbox mb-3">
                <input class="custom-control-input" id="{{$material->id}}" type="checkbox" name="material" value="{{$material->id}}">
                <label class="custom-control-label" for="{{$material->id}}">{{$material->name}} دارم </label>
            </div>
        </div>
    </a>
@endforeach

I want when one or some items checked in my controller for each request execute a db insert command.

what i must write in my controller?

    public function requests_new_post(Request $request)
{

    foreach ($request->material as $material) {

        DB::table('materials')->insert([

            ?????

        ]);
    }


    return redirect('/panel/addresses');
}
2
  • 2
    Side note: The two last </div> and </a> should swap places. Commented Sep 16, 2019 at 8:11
  • show us the model of materials. And the column where you want to save those checboxes. Commented Sep 16, 2019 at 10:01

1 Answer 1

2

Change,

name="material" 

To,

name="material[]"

This will allow you to pass an array of values.

Then in your controller when the form is submitted, you'd loop through the array of passed data:

foreach ($request->material as $material) {
     // $material will not contain the value of the submitted checkbox
    //  Run your query in here
}
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.