Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ponnydalen's avatar

Realationship between two tables

I have two tables: Bookings and Companies. When I create a new booking I then select a company (1,2 or 3)

In my bookings table I then have a company_id and in my company table I have a booking_id

When I submit I get the correct value in the company_id field (booking model)

When I go to the edit booking page I want to display information about the company that booking has.

So i've tried this:

Booking model:

  public function companies()
{
    return $this->belongsTo('App\Company', 'booking_id');
}

Company model:

  public function bookings()
{
    return $this->hasMany('App\Booking', 'company_id');
}

Controller:

$comp = Booking::with('companies')->get();
    dd($comp);

I get:

	  #items: array:75 [▼
0 => App\Booking {#1361 ▼
  +casts: array:1 [▶]
  #dates: array:2 [▶]
  #connection: "mysql"
  #table: "bookings"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:25 [▶]
  #original: array:25 [▶]
  #changes: []
  #classCastCache: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "companies" => null
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

If i try this

$comp = Booking::find($id);

I get the current booking id but with no relations? How should I do this?

0 likes
2 replies
jlrdw's avatar

@ponnydalen also watch the free laravel from scratch videos, Jeffrey will teach you this stuff, while you learn. He covers some basic relationships.

Please or to participate in this conversation.