Aronaman's avatar

sync many to many

hello guys

room parent has 5 room child. quantity count the room child show in front end. my question is if the customer choice quantity b/n 1 to 5 . let say the customer choice 3 and book the room. the first 3 room children save in pavot table of booking_Individual. how can i do this??

room(parent)->individualRoom(child) 1 to many relationship

$result=$room()->individiual_rooms_count()->get();

<div class="form-group">
            <strong>Quantity:</strong>
    

              <select  name="quantity" >   
                  @for($i=1; $i<=$result; $i++)  
                  <option  >{{$i}}</option>
                  @endfor
              </select>

    
        </div>

booking.php //model


    public function individualRooms()

    {
      return $this->belongsToMany(individualRoom::class,'booking__individualroom');
    }

storeBooking

$booking =$room->bookings()->create($booking);
 $booking->individualRooms()->sync([???]));

if the user select 2 quantity the store the first 2 individual room, if 3 quantity selected same the first 3 from the individualroom .....

0 likes
5 replies
ahmeddabak's avatar
$booking =$room->bookings()->create($booking);
$booking->individualRooms()->sync($room->individualRoom->modelKeys()));
ahmeddabak's avatar

if this answer solves you problem please mark this post as solved

Aronaman's avatar

@ahmeddabak thanks for the reply

can you give some example? modelKeys()??

$booking->individualRooms()->sync($room->individualRoom->modelKeys()));

and one more how to count multiple selected fields. 1selected, 2 or 3 based multiple

 {!! Form::select('quantity', $result,[], array('class' => 'form-control','multiple')) !!}
ahmeddabak's avatar
Level 47

$room->individualRoom returns an elequent collection, you can call the modelKeys on it to get the ids of all returned models and return an array, it is equivalent to ->pluck('id')->all().

then we take the array and sync it with $booking->individualRooms() relation.

In regard to your second question, you are using a third party package, which i do not know, here is a link to the documentation Drop-Down Lists, if you still could not find a solution, you can create a different discussion and many will help you

Please or to participate in this conversation.