【发布时间】:2019-06-04 08:24:08
【问题描述】:
我正在使用以下方法生成descriptions 的下拉列表,并将其对应的category 作为 optgroup。一个Category hasMany Descriptions。
所以在这种情况下,我的activeDescriptions 是我在Category 模型中的关系。
public static function descriptions()
{
static::active()
->with('activeDescriptions')
->orderBy('name', 'asc')
->get()
->each(function ($category) use (&$descriptions) {
$descriptions[$category->name] = $category->activeDescriptions->pluck('name', 'id')->sortBy('name')->toArray();
});
return $descriptions;
}
问题是,当一个类别没有任何描述时,我想把它排除在下拉列表之外。所以我正在考虑使用类似的东西:
->each(function ($category) use (&$descriptions) {
if($category->doesnthave('activeDescriptions'))
{
continue;
}
$descriptions[$category->name] = $category->activeDescriptions->pluck('name', 'id')->sortBy('name')->toArray();
});
但这显然不起作用,但我无法弄清楚如何实现这一点。因此,任何指针将不胜感激。
【问题讨论】:
-
顺便说一句,您在哪里生成
$descriptions数组/集合?你引用它,但没有说从哪里来