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

lukeify's avatar

Create a Eloquent relationship between a parent table and a lookup table?

I'm using Laravel 4.2 and I'm slowly building a complex website, and I've come across a relationship between two of my tables that I cannot immediately figure out how to map together with Eloquent. Here is the relevant schema:

table `missions`
mission_id (PK) | name           | launch_site_id (FK)
------------------------------------------------------
1              | someMission    | 1
2              | anotherMission | 3
3              | moreMissions   | 1

table `launch_sites`
launch_site_id (PK) | name   | location
------------------------------------------------------
1                  | Kwaj    | <some coordinate>
2                  | Florida | <some coordinate>
3                  | Russia  | <some coordinate>

As you can see, the table launch_sites is a lookup table for missions, and each mission has a single launch site (guaranteed).

I tried representing this with a hasOne & belongsTo relationship in Eloquent ORM:

class Mission extends Eloquent {
    public function launchSite() {
        return $this->hasOne('LaunchSite');
    }
}

class LaunchSite extends Eloquent {
    protected $table = 'launch_sites';

    public function mission() {
        return $this->belongsTo('mission');
    }
}

However, I quickly realized this would not work as a launch site does not "belong to" a mission. With this relationship, I get the error:

Column not found: 1054 Unknown column 'launch_sites.mission_id' in 'where clause' (SQL: select * from launch_sites where launch_sites.mission_id = 3 limit 1)

What relationship setup in Eloquent do I want so I can correctly query and fetch the launch site from a mission like so?

Mission::{{Some Query}}->with('launchSite'); 
0 likes
2 replies
themsaid's avatar

Hey, The relation should be as follow

class Mission extends Eloquent {
    public function launchSite() {
        return $this->belongsTo('LaunchSite', 'launch_site_id');
    }
}

class LaunchSite extends Eloquent {
    protected $table = 'launch_sites';

    public function missions() {
        return $this->hasMany('Mission', 'launch_site_id');
    }
}

Now the Mission belongs to only one LaunchSite, and a LaunchSite has many missions.

2 likes
pmall's avatar

I feel like it is a mission that "belongsTo" a launch site. Seems logical and your schema is made this way.

I guess also that a launch site will have many missions.

class Mission extends Eloquent {
    public function launchSite() {
        return $this->belongsTo('LaunchSite', 'launch_site_id');
    }
}

class LaunchSite extends Eloquent {
    protected $table = 'launch_sites';

    public function mission() {
        return $this->hasMany('mission', 'launch_site_id');
    }
}

Use belongsTo and hasMany parameters to specify custom foreign keys name.

Update : @themsaid omg I didn't see your reply and wrote exactly the same :D

1 like

Please or to participate in this conversation.