Mysql
 sql >> Database >  >> RDS >> Mysql

Come creare una query con due volte una connessione a una tabella in Laravel 5.3?

puoi anche usare il modello eloquente per definire la relazione.

Inoltre, per maggiori dettagli, visita https://laravel.com/docs/5.3/eloquent-relationships

crate two model --1st è "Voli"

<?php


class Flights extends Model
{
    protected $table = 'flights';

    /**
     * Get the From City detail.
     */
    public function fromCity()
    {
        return $this->hasOne('App\Models\City', 'Pana', 'from_city');
    }

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

}

Il secondo modello è "Città"

<?php
class City extends Model
{
    protected $table = 'city';
}

Ora per il recupero

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