Linking multiple model forms for editing in laravel

I am new to laravel. Maybe the question is not consistent with this problem. The problem is that. I have one database table for many, like a hotel and a room. One hotel can have several rooms. My goal:

 public function edit($id) {
    echo $id;
    // get the hotel
    $data['hotel'] = Hotel::find($id);
    $data['rooms'] = Room::where("hotelId", $id)->get();

    // show the edit form and pass the hotel
    return view('include.middle')->nest('main', 'hotel.edithotel', $data);
}

I get data from a table of rooms and hotels. Sure, there may be several rooms.

In sight. I want to get all the editing data for the hotel and room.

editHotel.blade.php

 //for hotel
 {{ Form::model($hotel, array('route' => array('hotel.update', $hotel->hotelId), 'method' => 'PUT')) }}

 //for room.  I want to run this in loop so that I can get data to edit for multiple rooms.
 {{ Form::model($rooms, array('route' => array('room.update', $rooms->roomId), 'method' => 'PUT')) }}

But I get an error like

 Undefined property: Illuminate\Database\Eloquent\Collection::$roomId (View: /Users/apple/hotelbooking/resources/views/hotel/edithotel.blade.php)

I set the primary keys in Room and Hotel Model.

Database schema.

  Hotel
  hotelId  | name             | address
  1        | example hotel       somewhere

  Room
  roomId   | hotelId   |  name  | price 
  1           1           Delux    $300
  2           1           Economy  $100
+4
source share
1 answer

laravel. , , laravel, undefined COLLECTION.

- Laravel, - ( )

, , , ,

{{ Form::model($rooms, array('route' => array('room.update', $rooms->roomId), 'method' => 'PUT')) }}

foreach.

@foreach($rooms as $room)
{{ Form::model($room, array('route' => array('room.update', $room->roomId), 'method' => 'PUT')) }}
@endforeach

,

+2

Source: https://habr.com/ru/post/1659081/


All Articles