1

I have a form where user select one or more checkboxes like this:

{!! Form::Label('faults', 'Please select one of the following that best describes your device') !!}
                                            <div class="form-group">                                            
                                                @foreach($faults as $fault)
                                                <div class="col-md-4">
                                                    <span><i class="fa fa-question-circle" rel="popover" data-content="{{$fault->fault_tooltip}}"></i></span>
                                                    <label class="checkbox-inline">
                                                    <input id="faults" type="checkbox" name="faults[]" value="{{$fault->id}}">{{$fault->fault}}
                                                    </label>
                                                </div>
                                                @endforeach
                                            </div>

Now in my controller I am getting the input for faults like this:

$faults = Input::get('faults');

This returns something an array contining the ID's for each fault like this:

    array:2 [▼
  0 => "37"
  1 => "36"
]

I need to loop through the array and find each row in my faults table matching each id from the array and get other values like $fault->name and $fault->price.

I know this is pretty easy but I am starting with coding and I can't get my head around.

Any help will be much appreciated.

Thanks

2 Answers 2

2
$slectedFaults = Input::get('faults');

if you have faults model in \App\ directory then do by following

$faults = Faults::whereIn('id',$slectedFaults)->get();

or do by foloowing way

 $faults = App\DB::table('faults')->whereIn('id',$slectedFaults)->get();

$faults have data selected from your database, then do the rest

$nameArray = [];
foreach($faults as $fault) {
  $nameArray[] = $fault->name;
  //do your thing here
}
print_r($nameArray); //it will print all fault names
Sign up to request clarification or add additional context in comments.

Comments

0

Do you have a Faults model? if so:

$faults = Faults::findMany(Input::get('faults'));

foreach($faults as $fault) {
   //do your thing here
}

Otherwise with a table:

$faults = DB::table('faults')
  ->select()
  ->whereIn('id',Input::get('faults'))
  ->get();

foreach($faults as $fault) {
   //do your thing here
}

2 Comments

Thanks for responding that quick! That works but if I try to echo $fault_name (inside the loop: $fault_name = $fault->name;) I only get 1 fault, but the user selected two.
if you do dd($faults) before the for loop which/how many faults do you see?

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.