Level 2
ok i solve it by changing
$category_id = Products::findOrFail($id);
$records=\DB::table('products')->Where('category_id','$category_id')->get();
to
$records = Products::where('category_id', $id)->get();
3 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
No query results for model [App\Products].
I have a table "products" which has a column "category_id" that a category Id of each category items. I want to show the items according to category Id
My controller is like this
public function category()
{
$recordsByCategories=\DB::table('products')
->select('categories','category_id', \DB::raw('count(*) as totalProducts'))
->groupBy('categories')
->get();
return view('dashboard.show',compact('recordsByCategories'));
}
**//I have received error in below function**
public function searchCategoryByTag($id)
{
$category_id = Products::findOrFail($id);
$records=\DB::table('products')->Where('category_id','$category_id')->get();
dd($records);
}
My routes is like this
Route::get('categorys/{id}' ,array(
'as'=>'category',
'uses'=>'GoodsController@searchCategoryByTag'));
My view is like this
@foreach($recordsByCategories as $recordsByCategory)
<a href="{{URL::route('category',$recordsByCategory->category_id)}}
" id="search">{{$recordsByCategory->categories}}</a>:{{$recordsByCategory->totalProducts}}
@endforeach
ok i solve it by changing
$category_id = Products::findOrFail($id);
$records=\DB::table('products')->Where('category_id','$category_id')->get();
to
$records = Products::where('category_id', $id)->get();
Please or to participate in this conversation.