No query results for model [App\Products] Laravel

后端 未结 5 2098
予麋鹿
予麋鹿 2020-12-20 21:45

In laravel5 i got following message(error)

No query results for model [App\\Products].

I have a table \"products\" which has a

相关标签:
5条回答
  • 2020-12-20 22:27

    **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();

    `$categroy_id ` 
    

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

    0 讨论(0)
  • 2020-12-20 22:34

    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.

    0 讨论(0)
  • 2020-12-20 22:42

    Try adding this member to your Product model class;

    protected $table = 'products';
    
    0 讨论(0)
  • 2020-12-20 22:46

    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);
    }
    
    0 讨论(0)
  • 2020-12-20 22:47

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

    0 讨论(0)
提交回复
热议问题