我有一個表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。我在想這個。 –