【问题标题】:Laravel, how to eliminate some of the "pluck" value for dropdown selection?Laravel,如何消除下拉选择的一些“采摘”值?
【发布时间】:2018-06-30 08:25:24
【问题描述】:

控制器

$getFurniture = Furniture::where('active', 1)->pluck('fur_name', 'id');
dump($getFurniture);

表格

如您所见,代码返回“家具”表中的所有值,我如何在 pluck 查询结果中消除“家具价格”表中包含的值?所以在这种情况下,用户可以选择“fur5”、“fur6”、“fur8”、“fur9”、“fur10”、“fur11”和fur12”

【问题讨论】:

  • furniturefurniture_prices 有什么关系?
  • furniture_prices 表由 fur_id 关联到家具表 id

标签: php sql laravel


【解决方案1】:

我假设您已经定义了关系。使用doesntHave() 方法:

Furniture::where('active', 1)->doesntHave('furniturePrices')->pluck('fur_name', 'id');

如果关系还不存在,先创建hasOne()hasMany()关系:

public function furniturePrices()
{
    return $this->hasMany(FurniturePrice::class, 'fur_id');
}

【讨论】:

  • 我应该把关系放在哪里?是在 Furniture(furnitures) 模型还是 FuniturePrice(furniture_prices) 模型中?
  • 哦,我的...就像一个魅力!直截了当的回答和解释,非常感谢您
  • 如果我可能会问,在原始查询中是否有替代方法?只是问
  • @learnprogramming 你总是可以对原始查询做同样的事情,但是如果你想让你的应用保持可维护性,you should use Eloquent where possible 我在我的最佳实践存储库中建议。
猜你喜欢
  • 2017-06-11
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
相关资源
最近更新 更多