2

There are two mysql tables 1.seats (id,number), 2.reservedseats(id,seat_id,sceering_id). I show all the seats of a specific sceering as checkboxes in show.blade:

{!!Form::model($screening,['method'=>'post', 'action'=> 
['ReserveController@store',$screening->auditorium->id]])!!}

<input type="hidden" name="screening_id" value="{{$screening->id}}">

@foreach($seats as $seat)

<label class="checkbox-inline">

{!!Form::checkbox('seat_id[]',$seat->id,null)!!} Number: {{$seat->number}}  

</label>

@endforeach

<div class='form-group'>

{!!Form::submit('Create Post',['class'=>'btn btn-primary '])!!}

</div>

{!!Form::close()!!}

When I click a checkbox it goes the the seat_id[] array. So I send a hidden input and an array with the seat_ids then I want to store in the reservedseats Mysql table. But in the store controller I have the problem. I'm trying something like:

public function store(Request $request){

 $screening_id = $request->screening_id;

 $seat_ids = $request->seat_id;

  foreach($seat_ids as $seat_id){

    Seatreserved::create($seat_id,$screening_id);

   }
}

So it not working but how can I solve that?

3 Answers 3

6

Try this code

public function store(Request $request)
{
    $screening_id = $request->screening_id;
    $seat_ids = $request->seat_id;

    foreach($seat_ids as $seat_id) {
        Seatreserved::create([
            'seat_id' => $seat_id,
            'screening_id' => $screening_id
        ]);
    }
}

Also you can use

public function store(Request $request)
{
    $screening_id = $request->screening_id;
    $seat_ids = $request->seat_id;
    
    $data = [];
    foreach($seat_ids as $seat_id) {
        $data[] = [
            'seat_id' => $seat_id,
            'screening_id' => $screening_id
        ];
    }
    Seatreserved::insert($data);
}

That is better way to perform this as it will interact with database for once.

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

Comments

0

You can also create a new instance of your model to store values.

Example:

foreach($seat_ids as $seat_id) {
    $reserved = new Seatreserved();

    $reserved->seat_id = $seat_id;
    $reserved->screening_id = $screening_id;

    $reserved->save();
}

Comments

0
    $input = $request->all();
        
    $invoice = Invoice::create($input);
    
    foreach($input['products'] as $key => $value){            
        $product['product_id'] = $value['product_id'];
        $product['amount'] = $value['amount']; 
        $product['invoice_id'] = $invoice->id;
        InvoiceProducts::create($product);
    }

    return redirect()->route('invoices.index')->with('success', 'Invoice Added Successfully');

}

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
There is a return here without a function definition, which makes this look incomplete. Please fix it.

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.