我正在尝试找到一种方法,当用户填写带有音乐或视频类别的搜索栏时。提交后,这将触发查询。这将输出特定类别的所有帖子。我尝试使用内部联接来加入2个表的加入和类别。我被卡住了,因为这不起作用。请帮帮我。
public function getSearch(Request $request){
$categories = Category::all();
$search = $request->input('search');
$accessions = DB::table('accessions')
->leftJoin('categories', 'accessions.category_id', '=', 'categories.id')
->where('category_name','=','%'.$search.'%')
->get();
return view('accessions.index')->withAccessions($accessions)->withCategories($categories);
}

//tell relationship that an accession belongs to a category
public function category(){
return $this->belongsTo('App\Category');
}

//this shows relationship that a category has many accessions
public function accessions(){
return $this->hasMany('App\Accession');
}

答案 0 :(得分:0)
我错过了什么。应该是categories.category_name
public function getSearch(Request $request) {
$categories = Category::all();
$search = $request - > input('search');
$accessions = DB::table('accessions') - > leftJoin('categories', 'accessions.category_id', '=', 'categories.id') - > where('**categories.category_name**', 'like', '%'.$search.
'%') - > get();
return view('accessions.index') - > withAccessions($accessions) - > withCategories($categories);
}