php 类 Illuminate\Database\Eloquent\Builder 的对象无法在 Laravel 5.1 中转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33865289/
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
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string in laravel 5.1
提问by Pawan Dongol
I am googling and try form nxt two days and it try to debug it so my code which get the id of data from the table was:-
我在谷歌上搜索并尝试表单 nxt 两天,它尝试调试它,所以我从表中获取数据 ID 的代码是:-
$term=category::get(['id']);
Use that $varibale for getting the data from database as per id which is make foreign key and the code are as follows:-
使用 $varibale 从数据库中获取数据,根据 id 这是外键,代码如下:-
$categories = HelpCenter::whereHas('category', function($category) use ($term)
{
$category->where('category_id','=',$category);
})
->take(5)->get();
回答by Thomas Kim
This line of code does not make sense:
这行代码没有意义:
$category->where('category_id','=',$category);
$category
is an Eloquent Builder instance so what you are effectively doing is this:
$category
是一个 Eloquent Builder 实例,所以你有效地做的是:
EloquentBuilder->where('category_id', '=', EloquentBuilder);
I'm assuming the second $category
variable was meant to be $term
like this:
我假设第二个$category
变量是$term
这样的:
$category->where('category_id','=',$term);
But that will still cause some issues, which I'll explain in a bit. First though, I think you should rename the variable so it makes a bit more intuitive sense. Instead of $category
, name it $query
so things don't get mixed up.
但这仍然会导致一些问题,我将在稍后解释。不过,首先,我认为您应该重命名变量,以便更直观。取而代之的是$category
,给它命名,$query
这样事情就不会混淆了。
$categories = HelpCenter::whereHas('category', function($query) use ($term)
{
$query->where('category_id', '=', $term);
})
->take(5)->get();
Next, we need to tackle a few more things.
接下来,我们需要处理更多的事情。
- In your example, the
$term
variable is never used. I am assuming that it should be an array of category ids. - I'm also assuming that
$term
is what you want to pass into thewhere
method.
- 在您的示例中,
$term
从未使用过该变量。我假设它应该是一个类别 id 数组。 - 我还假设这
$term
就是您想要传递给where
方法的内容。
To tackle #1, you are currently getting a collection of category instances. You're actually getting every single category in your database. I'm not sure if that's your intention, but either way, you probably still want an array of category ids. As @smartrahat suggested, use the pluck
method.
为了解决#1,您目前正在获取类别实例的集合。您实际上获得了数据库中的每一个类别。我不确定这是否是您的意图,但无论哪种方式,您可能仍然需要一组类别 ID。正如@smartrahat 建议的那样,使用该pluck
方法。
$term = category::get(['id'])->pluck('id');
To tackle #2, you want to find all categories that match the ids you are passing. You can use the whereIn
method for that.
要解决 #2,您需要找到与您传递的 ID 匹配的所有类别。您可以使用该whereIn
方法。
$query->whereIn('category_id', $term);
All together, it should look something like this:
总之,它应该是这样的:
$term=category::get(['id'])->pluck('id');
$categories = HelpCenter::whereHas('category', function($query) use ($term)
{
$query->whereIn('category_id', $term);
})
->take(5)->get();
回答by djt
You don't need to use both take()
and get()
. Just just one or the other.
您不需要同时使用take()
和get()
。只是其中之一。
回答by Bhargav Kaklotara
$product = $data->with('productimages')->with('categories')->where('id', $id)->first();