【发布时间】:2020-03-25 11:32:30
【问题描述】:
我有模型材料,它有
protected $with = ['costPrices'];
public function costPrices(){
return $this->hasMany(CostPrice::class);
}
cost_prices 表有多个数量
//create_cost_prices_table
Schema::create('cost_prices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('material_id');
$table->unsignedBigInteger('supplier_id');
$table->integer('quantity');
$table->timestamps();
});
我想以另一种方式获取(材料不足)选择所有材料表格材料表,其中所有成本价格表的数量总和
【问题讨论】:
-
看看
whereHas:laravel.com/docs/6.x/… -
当我使用
$materials = Material::whereHas('costPrices', function($q) { $q->havingRaw('SUM(quantity) > 4'); })->get();时出现错误SQLSTATE[42000]: Syntax error or access violation: 1140 In aggregated query without GROUP BY, expression