我对口才有点意见。我有三张桌子。Users,phrases和phrase_user。短语用户包含列:id, phrase_id, user_id,我需要选择带有枢轴表的短语。使用SQL的操作将类似于.
SELECT phrase FROM phrases
LEFT JOIN phrase_user ON phrases.id = phrase_user.phrase_id
LEFT JOIN users ON phrase_user.user_id = $userId这是我在控制器中的索引函数
$phrases = DB::table('phrases')->orderBy('id','desc')->Paginate(5);
$user = Auth::user();
return view('pages.phrases.index')->with(['phrases' => $phrases, 'user' => $user]);我也用模型写了这些函数..。短语模型:
public function users()
{
return $this->belongsToMany('App\User')->withTimestamps();
}用户模型:
public function phrases()
{
return $this->belongsToMany('App\Phrase')->withTimestamps();
}在这种情况下,分页器无法正常工作,因为$phrases = DB::table('phrases')->orderBy('id','desc')->Paginate(5);存在一些问题。
总结:我只需要在短语页面显示我的短语。每个用户都必须有自己的短语。
发布于 2020-08-19 09:51:25
$userId = 2;
$phrases = DB::table('phrases')->whereHas('users', function ($q) use ($userId) {
$q->whereId($userId);
})->orderBy('id','desc')->Paginate(5);
$user = Auth::user();
return view('pages.phrases.index')->with(['phrases' => $phrases, 'user' => $user]);发布于 2020-08-19 09:48:46
尝试使用小写分页
$phrases = DB::table('phrases')->orderBy('id','desc')->paginate(5);https://stackoverflow.com/questions/63484190
复制相似问题