php 没有模型 [App\Products] Laravel 的查询结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30585912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:53:56  来源:igfitidea点击:

No query results for model [App\Products] Laravel

phpmysqllaravellaravel-5

提问by Ranjeet Karki

In laravel5 i got following message(error)

在 laravel5 中,我收到以下消息(错误)

No query results for model [App\Products].

I have a table "products" which has a column "category_id" that a category Idof each category items. I want to show the items according to category Id

我有一个表“ products”,其中有一列“ category_id”表示category Id每个类别项目。我想根据以下内容显示项目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

回答by AddWeb Solution Pvt Ltd

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.

在这里你弄错了'$category_id', 删除那个单引号。

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

回答by kaleazy

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.

检查您的 API 路由。确保没有任何内容与该端点竞争会导致/products. 这经常发生在我身上。

回答by Datha Reico

**You should change form **

**你应该改变形式**

 $category_id = Products::findOrFail($id);
 $records=\DB::table('products')->Where('category_id','$category_id')->get();

change to this$records = Products::where('category_id', $id)->get();

改成这个$records = Products::where('category_id', $id)->get();

`$categroy_id ` 

**is get object form your products table instance of **$category_idto $category_id->id

**就是让物体形式的**您的产品表实例$category_id$category_id->id

回答by aethergy

If $id equals category id, do only $records = Products::where('category_id', $id)->get();in searchCategoryByTag.

如果 $id 等于类别 id,则仅$records = Products::where('category_id', $id)->get();在 searchCategoryByTag 中执行。

回答by Terna K

Try adding this member to your Product model class;

尝试将此成员添加到您的 Product 模型类中;

protected $table = 'products';