2

I have this form in the view:

Your Name: <input type="name" name="name" id="name" ><br><br>
Your Contact No. <input type="contact" name="contact" id="contact" ><br><br>
Todays date:<mark>{{$mytime->format('Y-m-d, l')}}</mark><br><br>
<br><br>BOOK SEATS:<br>
@foreach($seats as $seat)
@if($seat->available=='1')
<input type="checkbox" name="check[]" value="{{$seat->book}}" >{{$seat->book}}<br><br>
 @else
<input type="checkbox" name="check[]" value="{{$seat->book}}" disabled>{{$seat->book}}<br><BR>
@endif
@endforeach
<button type="submit" class="btn btn-default">Book</button>
</form>

this form redirects to this controller which inserts the name contact and the value of checkbox checked with loop. This way, if i check two values and submit the form, due to for loop two rows will be inserted with same name contact but with different booked_seats and id(i used id as auto increment). Is there any way of inserting the checked values into booked_seats with only one row. OR making the id same for any rows inserted(checked values) at the time.

 public function book(Request $request)
  {
    $name=$request->get('name');
    $contact=$request->get('contact');
    $check=$request->get('check');
    $totalcheckboxchecked=sizeof('$check'); 
for($i=0;$i<=$totalcheckboxchecked;$i++)
{
    if (array_key_exists($i,$check) )
{   
    $booked=$check[$i];
    $book=bus::insert(['name'=>$name,'contact'=>$contact, 'booked_seats'=>$booked, 'active'=>'1']);
    seats::where('book',$booked)->update(['available'=>'0']);

}
}
        return redirect('/bus')
            ->with('message','booked successfully!!!');

}

1 Answer 1

1

You can store the checked values in database using serialize

$booked = serialize ($check);

Then you can save in database like this without using for loop

$book=bus::insert(['name'=>$name,'contact'=>$contact, 'booked_seats'=>$booked, 'active'=>'1']);
    seats::where('book',$booked)->update(['available'=>'0']);

To rretrieve the values use unserialize

unserialize($booked);

Here is the full code.

public function book(Request $request)
  {
    $name=$request->get('name');
    $contact=$request->get('contact');
    $check=$request->get('check');
    $checkarray = serialize($check);
    $totalcheckboxchecked=sizeof('$check'); 
for($i=0;$i<=$totalcheckboxchecked;$i++)
{
    if (array_key_exists($i,$check) )
{   
    $booked=$check[$i];

    seats::where('book',$booked)->update(['available'=>'0']);

}
$book=bus::insert(['name'=>$name,'contact'=>$contact, 'booked_seats'=>$checkarray, 'active'=>'1']);
}
        return redirect('/bus')
            ->with('message','booked successfully!!!');

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

6 Comments

this is good. but a:2:{i:0;s:2:"B1";i:1;s:2:"B2" is inserted in database, i want B1 and B2 to later compare these values. i wrote the update query seats::where('book',$booked)->update(['available'=>'0']);
separate the two queries. For update you can do what you were doing previously. At least it wont insert a new row.
I want to upadate if $booked matches the value stored in book column (book column has values B1, B2, B3,...) previously B1, B2, B3... were inserted now a:2:{i:0;s:2:"B1";i:1;s:2:"B2"
you have to unserialize it. Update it with a foreach loop, create a new array and before storing to database serialize it again. hope i am clear.
the values to insert into bus table using checkbox are fetched from another table seats. now if the checked values equals to values stored in 'book' column of seats then i want to update the available column of seats table to zero. used $update=unserialize($booked); seats::where('book',$update)->update(['available'=>'0']); but failed.
|

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.