在 laravel 5.5 中调用未定义的方法 whereHas()
Call to undefined method whereHas() in laravel 5.5
我正在将我的站点升级和重构到 laravel 5.5,这段代码目前给我带来了问题。我在 laravel github 文档中进行了搜索,但没有发现任何可能影响这一点的重大更改。
我想做的是与我网站中的部分相关,在每个食谱页面中我想显示更多具有相同类别的食谱。
这是我的代码:
public static function getRelatedRecipes($recipe){
$related_category_ids = $recipe->category()->pluck('categories.id');
return $relatedRecipes =
Recipe::whereHas('categories', function ($q)use($related_category_ids){
$q->whereIn('category_id', $related_category_ids);
})
->where('id', '<>', $recipe->id)
->take(4)
->inRandomOrder()
->get();
}
这是配方模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Recipe extends Model
{
protected $guarded=[];
/**
* Get the route key name for Laravel.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
public function category()
{
return $this->belongsToMany('App\Category');
}
}
可能是什么问题?
谢谢,
P.S
如果您认为需要任何其他代码来解决此问题,请告诉我,我会在此处 post。 :)
首先要确定,Recipe
你在方法中使用的是模型,所以不是
Recipe::whereHas('categories', function ($q)use($related_category_ids){
使用
\App\Recipe::whereHas('categories', function ($q)use($related_category_ids){
另一件事是这种categories
关系。在模型中你没有 categories
关系,你只有 category
关系
我正在将我的站点升级和重构到 laravel 5.5,这段代码目前给我带来了问题。我在 laravel github 文档中进行了搜索,但没有发现任何可能影响这一点的重大更改。
我想做的是与我网站中的部分相关,在每个食谱页面中我想显示更多具有相同类别的食谱。
这是我的代码:
public static function getRelatedRecipes($recipe){
$related_category_ids = $recipe->category()->pluck('categories.id');
return $relatedRecipes =
Recipe::whereHas('categories', function ($q)use($related_category_ids){
$q->whereIn('category_id', $related_category_ids);
})
->where('id', '<>', $recipe->id)
->take(4)
->inRandomOrder()
->get();
}
这是配方模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Recipe extends Model
{
protected $guarded=[];
/**
* Get the route key name for Laravel.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
public function category()
{
return $this->belongsToMany('App\Category');
}
}
可能是什么问题?
谢谢,
P.S
如果您认为需要任何其他代码来解决此问题,请告诉我,我会在此处 post。 :)
首先要确定,Recipe
你在方法中使用的是模型,所以不是
Recipe::whereHas('categories', function ($q)use($related_category_ids){
使用
\App\Recipe::whereHas('categories', function ($q)use($related_category_ids){
另一件事是这种categories
关系。在模型中你没有 categories
关系,你只有 category
关系