【发布时间】:2019-10-02 21:36:45
【问题描述】:
嗯,我必须在我的数据库 Bus 和 Station 中使用表,它们有一对多的关系,这是场景的问题-
- 一个车站可以有多辆公交车。
- 5 到 10 辆公交车不需要分页,但如果 Station 有 100 或 1000 辆公交车怎么办。这种方法太可怕了。
这就是我所做的......
与车站的巴士关系
public function station()
{
return $this->belongsTo(Station::class);
}
车站与巴士的关系
public function bus()
{
return $this->hasMany(Bus::class);
}
这里是我在 Index Controller
中所做的// getting ther user id
$id= Sentinel::getUser()->id;
// fiding the statioin_id of that user_id and assigning it to variable
$user = User::find($id);
$s_id = $user->station_id;
// fiding the station which tht station_id belongs to and assign it to variable
$station = Station::find($s_id);
$stations_id = $station->id;
// getting buses of that specific station_id we just get and paginating it
$stationbus = Station::find($stations_id)->bus()->paginate();
return view('bus.index')->with('buses', $stationbus->bus);
这就是索引视图
当我执行上面的代码时会弹出这个错误
未定义属性:Illuminate\Pagination\LengthAwarePaginator::$bus
再一次,我的代码除了分页部分之外工作正常。
【问题讨论】:
-
在您的
->with()方法中,$stationbus->bus不应该只是$stationbus吗? -
@TimLewis 我怎么错过了!谢谢,伙计,你拯救了我的一天。
-
不用担心。它发生了 :) 如果您不知道
LengthAwarePaginator is是什么,则该错误有点模糊,但通常当您看到“未定义的属性 X”时,查找$something->X,您应该会找到罪魁祸首。干杯!
标签: laravel laravel-5 pagination relationship