我有一个表TBL_POST
用于存储博客文章。一个帖子可以分配到多个类别,有一列cat_id存储类似ID的逗号分隔模式,如2,4,6。我想用FIND_IN_SET()方法在这一行Laravel 5.4原始联接查询
->leftJoin(TBL_CAT.' as c', 'p.cat_id', '=', 'c.id')
显示相关的类别名称。我怎样才能做到这一点?
public static function getPostWithJoin($status="")
{
$query = DB::table(TBL_POST .' as p')
->select('p.id','p.post_title','p.post_status','u.name as author','c.name as cat_name','p.updated_at')
->leftJoin(TBL_ADMINS.' as u', 'p.post_author', '=', 'u.id')
->leftJoin(TBL_CAT.' as c', 'p.cat_id', '=', 'c.id')
->where('p.post_type','post');
if($status!="all") {
$query->where('p.post_status',$status);
}
$query->orderby('p.id','DESC');
$data = $query->paginate(20);
return $data;
}
其糟糕的方法来保存多对多的关系,最好创建一个第三表tbl_cat_post你只能从这里保存post_id和cat_id你可以加入,类别和张贴表, – umefarooq
谢谢你的建议@umefarooq。我在想这个。 –