Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - No query results for model [AppProducts] Laravel

In laravel5 i got following message(error)

No query results for model [AppProducts].

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check your API routes. Make sure nothing is competing with that endpoint that would cause a 404 error on the request for /products. This happens to me often.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...