laravel这个关联查询怎么写?
关联关系是这样的:
<code>users表->articles表 ,一对多关系。 categories表->articles表,一对多关系。 </code>
查询:
<code> public function index()
{
$user=\Auth::user();
//1、查询当前登录用户的文章.
//2、文章对应的类别.
//用的下面这样一条语句:
$articles = $user->articles->with('category');
return view('index', compact('articles'));
}</code>报错:
问题:
index()方法中的查询语句应该怎么写?
laravel这个关联查询怎么写?
关联关系是这样的:
<code>users表->articles表 ,一对多关系。 categories表->articles表,一对多关系。 </code>
查询:
<code> public function index()
{
$user=\Auth::user();
//1、查询当前登录用户的文章.
//2、文章对应的类别.
//用的下面这样一条语句:
$articles = $user->articles->with('category');
return view('index', compact('articles'));
}</code>报错:
问题:
index()方法中的查询语句应该怎么写?
换成这样的,将article改成方法,这样返回的就不是结果,而是query builder:$articles = $user->articles()->with('category')->get();
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号