-1
我有以下表:Laravel显示数据3表
flights(id, title, number)
stations(id, title)
flight_price(id, price, flight_id, stationA_id, stationB_id)
flight_station(flight_id, station_id)
flight_station
是一个透视表。
我的模型:
class Flight extends Model
{
public function stations()
{
return $this->belongsToMany('App\Model\Station', 'flight_station', 'flight_id', 'station_id')
}
// To attach data
public function prices()
{
return $this->belongsToMany('App\Model\FlightPrice', 'flight_price', 'flight_id', 'stationA_id')
->withPivot('price');
}
public function price()
{
return $this->hasMany('App\Model\FlightPrice', 'flight_id');
}
}
// Station
class Station extends Model
{
public function flights()
{
return $this->belongsToMany('App\Model\Flight', 'flight_station', 'station_id', 'flight_id');
}
}
class FlightPrice extends Model
{
protected $table = 'flight_price';
public function flights()
{
return $this->belongsToMany('App\Model\Flight', 'flight_price');
}
}
我需要下一个结果(通过ID飞行找到):
|stationA_id|stationA_title||stationB_id|stationB_title|price|
什么你“需要”是真的不清楚。请更准确地描述预期的结果。你能提供迄今为止所做的一些工作吗? – louisfischer
如何获取数据并显示它。如何链接所有使用雄辩关系? –
有一个非常好的[一系列免费视频Laravel Laraasts开始](https://laracasts.com/series/laravel-from-scratch-2017) – louisfischer