How to create query with twice a connection to a table in Laravel 5.3?

前端 未结 2 454
粉色の甜心
粉色の甜心 2020-12-21 12:07

I need get two city names with one query:

For example:

City table:

+---------+----------+
|  Pana   |   Name   |
+---------+----------+
|   T         


        
2条回答
  •  时光说笑
    2020-12-21 12:44

    you can also use the eloquent model with defining the relationship.

    Also for more detail visit https://laravel.com/docs/5.3/eloquent-relationships

    crate two model -- 1st is "Flights"

    hasOne('App\Models\City', 'Pana', 'from_city');
        }
    
        /**
         * Get the To city state.
         */
       public function toCity()
       {
            return $this->hasOne('App\Models\City', 'Pana', 'to_city');
       }
    
    }
    

    2nd Model is "City"

    Now for fetching

    Flights::where(id, $id)->with('toCity', 'fromCity')->get();
    

提交回复
热议问题