PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 10, 2022

[FIXED] No query results for model [App\Products] Laravel

 February 10, 2022     laravel, laravel-5, mysql, php     No comments   

Issue

In laravel5 i got following message(error)

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');
          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

Solution

I know it's too late, but for the help for you(May be) and other people:

Here you mistaken the '$category_id', remove that single quotes.

public function searchCategoryByTag($id)
{
    $category_id = Products::findOrFail($id);
    $records=\DB::table('products')->Where('category_id', $category_id);
}


Answered By - AddWeb Solution Pvt Ltd
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing