Php Laravel 5.x,其中多对多带有额外的枢轴柱

Php Laravel 5.x,其中多对多带有额外的枢轴柱,php,laravel,eloquent,many-to-many,Php,Laravel,Eloquent,Many To Many,我在食谱和配料之间设置了一个多对多数据透视表。对于一种特殊的配料,我想要一份配方清单 $ingredientID = 99; $recipies = Recipe::whereHas('ingredients', function ($q) use ($ingredientID) { $q->where('id', '=', $ingredientID); })->get(); 这就行了。但在我的例子中,pivot表中有一个额外的列用于“description”。例如,一

我在食谱和配料之间设置了一个多对多数据透视表。对于一种特殊的配料,我想要一份配方清单

$ingredientID = 99;

$recipies = Recipe::whereHas('ingredients', function ($q) use ($ingredientID) {
    $q->where('id', '=', $ingredientID);
})->get();
这就行了。但在我的例子中,pivot表中有一个额外的列用于“description”。例如,一个特定的“饼干”配方和“鸡蛋”成分连接可能有这样的描述:“确保鸡蛋对于这个配方来说是真正新鲜的”

那么,如何使用此查询返回额外的pivot表列呢?我尝试进入模型文件并添加“withPivot”调用,如下所示:

public function ingredients() 
{
    return $this->belongsToMany('App\Ingredient')->withPivot("description");
}

但这不起作用。你知道我如何恐吓我的whereHas查询,让它说出这个额外的数据透视表列吗?

要访问
说明
数据透视列,请执行以下操作:

$recipies->first()->ingredients->first()->pivot->description;

自使用withPIvot()以来,whereHas查询已包含该信息。您只需调用(从$Recipes中选择的模型,比如在foreach中)$recipie->pivot->description。我已经尝试过了——从whereHas查询的结果中,我做了这样一个循环:foreach($Recipes as$index=>$recipie){Log::info(“description:”.$recipie->pivot->description);}但是我得到了“正在尝试获取空对象的属性”