I need help to convert a complex SQL query into eloquent. I have models for all tables (search & airports) in order to join them with With().
The query is:
"SELECT *, fromTable.iata AS from_airport_iata, toTable.iata AS to_airport_iata
FROM ( SELECT * FROM searches ORDER BY searches.id DESC) AS tmp_table
LEFT JOIN airports fromTable ON fromTable.id = tmp_table.from_airport_id
LEFT JOIN airports toTable ON toTable.id = tmp_table.to_airport_id
GROUP BY tmp_table.to_airport_id
ORDER BY tmp_table.id DESC"
For now I have the following (obviously not doing what I want yet):
$search = new Search;
$search->with('joinAirportFrom')->with('joinAirportTo')->get();
The Search.php Model is:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Search extends Model
{
public $timestamps = false;
public function joinAirportFrom()
{
return $this->hasOne('App\Airport','id','from_airport_id');
}
public function joinAirportTo()
{
return $this->hasOne('App\Airport','id','to_airport_id');
}
}
The Airport.php Model is:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Airport extends Model
{
//
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire