Search code examples
phplaravelforge

Laravel One to Many Relationship not working


I am doing my assignment for a hotel booking system. I have a table named bookings and another table room. I did a one to many relations between them I want to return the room name to the home view but it is always showing Trying to get property of non-object. This is my code. BOOKING Controllerclass GuestBookingController extends Controller

public function new()
{
    $rooms = Room::all();
    $guests = Guest::all();
    return view('guestbookings.new', compact('rooms','guests'));
}

public function store(Request $request)
{
    $validatedData = $request->validate([
        'checkin_dtime' => 'required',
        'checkout_dtime' => 'required',
        'id_number' => 'required|unique:guests',
        'mobile' => 'required',
         ]);

    $result = Booking::where('checkin_dtime', '<=',$request->checkin_dtime)->where('checkout_dtime', '>=',$request->checkout_dtime)->where('room_id',$request->room_id)->first();
   if(!$result){
   $bookings = new Booking;
   $bookings->checkin_dtime = $request->input('checkin_dtime');
   $bookings->checkout_dtime = $request->input('checkout_dtime');
   $bookings->user_id = auth()->user()->id;
   $bookings->save();
   $guests = new Guest;
   $guests->id_number = $request->input('id_number');
   $guests->mobile = $request->input('mobile');
   $guests->save;

}
  return redirect('home');

Home view

 <table class="table table-striped">
                    <thead>
                        <tr>
                          <td>ID</td>
                          <td>Room Name</td>
                          <td>Check-In</td>
                          <td>Check-Out</td>
                          <td>Status</td>
                          <td>Action</td>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($bookings as $booking)
                        <tr>
                            <td>{{$booking['id']}}</td>

                            <td>{{$booking->room->room_name}}</td>

                            <td>{{$booking['checkin_dtime']}}</td>
                            <td>{{$booking['checkout_dtime']}}</td>
                            <td>

Booking Model

 public function room()
{
    return $this->belongsTo(Room::class, 'room_id');
}
public function user(){
    return $this->belongsTo(User::class);
}

Solution

  • this is your controller code as per the question:

    $bookings = new Booking;
    $bookings->checkin_dtime = $request->input('checkin_dtime');
    $bookings->checkout_dtime = $request->input('checkout_dtime');
    $bookings->user_id = auth()->user()->id;
    $bookings->save();
    

    you are not adding any room_id column. that means your room_id is null at database. so when you are trying to call relationship, eloquent is unable to build the relationship and you are getting the error. your code should be:

    $bookings = new Booking;
    $bookings->checkin_dtime = $request->input('checkin_dtime');
    $bookings->checkout_dtime = $request->input('checkout_dtime');
    $bookings->room_id = $request->input('room_id');
    $bookings->user_id = auth()->user()->id;
    $bookings->save();