在 laravel 的文本框中搜索时将显示某个类别的所有帖子的内部联接

inner join that will display all posts of a category when searched in a textbox in laravel

我正在尝试找到一种方法,当用户用音乐或视频等类别填充搜索栏时。一旦提交,这将触发一个查询。这将输出特定类别的所有帖子。我尝试使用内部联接来联接 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');
    }

我错过了一些东西。应该是 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);


}