Okay, so I have reviewed your code & following things need to be corrected.
1 . Line number 101, wrong variable definition change this
@foreach($subcategories as SubCategory)
<h1 class="f22 gr">{{ $subcategory->csname}}</h1>
@endforeach
To this
@foreach($subcategories as $subcategory)
<h1 class="f22 gr">{{ $subcategory->csname}}</h1>
@endforeach
2 . line number 109, there is some extra character remove it
Route::get('search-categories/{cid}', 'SubCatController@index');
3 . Line number 43, you have not passing the $cid parameter.
public function index($cid)
{
$category = Category::find($cid);
$subcategories= Category::find($cid)->subcategories;
return view('search-categories')->withCategory($category)->withSubcategory($subcategories);
}
4 . Last but not least you must set column 'cid' as foreign key in order to use laravel relationships. So you have two options now, one is set cid as FK or use direct DB class to get subcategories.
First option, make cid as Fk like this
ALTER TABLE categories_sub
ADD CONSTRAINT FK_cid
FOREIGN KEY (cid) REFERENCES categories(cid);
Second option, If you cant set foreign key go to your SubCatController.php change code like below.
public function index($cid)
{
$category = Category::find($cid);
$subcategories= SubCategory::where('cid',$cid)->get();
return view('search-categories')->withCategory($category)->withSubcategories($subcategories);
}
All the best! let me know if you still facing any issues.