呼叫成员函数where()在null错误上

发布于 2025-02-13 10:35:22 字数 1783 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2025-02-20 10:35:22

这个语法是不正确的!

阅读此文档以获取():
https://laravel.com/docs/docs/9.x/eloquent#检索单个模式

阅读此文档有关():
<条款

我的描述:

当您想找到一个product_category行时,必须编写此(例如,使用ID 7的product_category):

$product_category=product_category::find(7);

$product_category=product_category::where('id', 1)->first();

当您使用时first()函数在代码末尾,它返回一行,这是第一行。在代码末尾使用get()函数时,它返回查询中的一组行。
当您使用find()且其返回值为null时,您无法调用where()之后!

我认为您必须这样写您的控制器:

public function productCategory($id)
{
   $product_category=product_category::find($id);
   return view('product_category', compact('product_category'));
}

我建议您读过有关Laravel的雄辩:
https://laravel.com/docs/9.x/eloquent

以及关于Laravel查询构建器:
https://laravel.com/docs/9.x/ceries

this syntax is not correct!

read this document for find():
https://laravel.com/docs/9.x/eloquent#retrieving-single-models

read this document for where():
https://laravel.com/docs/9.x/queries#where-clauses

my description:

when you want to find a single product_category row you must write this (for example product_category with id 7):

$product_category=product_category::find(7);

or

$product_category=product_category::where('id', 1)->first();

when you use first() function at the end of your code, its return a single row and that is the first row. when using get() function at the end of your code, its return a set of rows that are in your Query.
when you use find() and its return value is null, you cant call where() after that!

I think you must write your controller like this:

public function productCategory($id)
{
   $product_category=product_category::find($id);
   return view('product_category', compact('product_category'));
}

I suggest you read about Laravel Eloquent much:
https://laravel.com/docs/9.x/eloquent

and about Laravel Query builder:
https://laravel.com/docs/9.x/queries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文