user3681740 user3681740 - 1 year ago 84
MySQL Question

laravel table related to only two elements on another table

I have two tables: trips and cities.

A trip should be only between two cities. (from "city A" to "city B").

In trips table, I put 2 foreign keys (city_A and city_B) referencing to "id" of cities table.

I dont know how to design this in laravel to get an optimised code.
I want to know what kind of relationship to use (hasOne or belongsToMany), and how can I retreive cities informations from tripController.

Answer Source

You should use belongsToMany() (many to many) with extra pivot columns here. withPivot() method will help you.

You may want to read about querying relations and eager loading.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download