【发布时间】:2017-10-13 10:08:46
【问题描述】:
我有以下表格:
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|
【问题讨论】:
-
你“需要”什么真的不清楚。请更准确地描述预期结果。你能提供一些你到目前为止所做的工作吗?
-
如何获取数据并显示。如何链接所有使用雄辩的关系?
-
如果我这样做: $flight = Flight::with('price', 'stations')->find($id); foreach($flight->price as $value) - 我没有得到数据站标题。
标签: php mysql laravel laravel-5 eloquent