刚学 Laravel 对 ORM 操作有点不熟悉,
想请教一下,
我有两个表,分别为 posts,users
并在 posts model 做了关联.
public function author(){
return $this->belongsTo('App\User','user_id','id');
}
想要写一个搜索功能,透过搜索作者,找到所有发文,
ORM 方式该怎做呢? 下面方式似乎找不到
$posts = Post::query();
$posts->orWhere('name', '=', "{$keyword}");
还是只能用查询构造器方式来写呢?
$posts = Post::leftjoin('users', 'posts.user_id', '=', 'users.id');
这样虽然是能运行,也能加入 where 条件做搜索.
但感觉似乎是用 Query Builder 完成的,并非使用关联
这样关联似乎是否就没意义了.
想请教一下,
我有两个表,分别为 posts,users
并在 posts model 做了关联.
public function author(){
return $this->belongsTo('App\User','user_id','id');
}
想要写一个搜索功能,透过搜索作者,找到所有发文,
ORM 方式该怎做呢? 下面方式似乎找不到
$posts = Post::query();
$posts->orWhere('name', '=', "{$keyword}");
还是只能用查询构造器方式来写呢?
$posts = Post::leftjoin('users', 'posts.user_id', '=', 'users.id');
这样虽然是能运行,也能加入 where 条件做搜索.
但感觉似乎是用 Query Builder 完成的,并非使用关联
这样关联似乎是否就没意义了.