如何在Laravel Eloquent whereHas查询中使用聚合

时间:2015-09-03 18:09:45

标签: php laravel eloquent

我的模型按以下方式设置:

Product hasMany ProductOptionProductOption belongsTo一个Option

我正在尝试查询ProductProductOption个,这些Option属于Oversized Shipping个,其名称中不包含字符串Product::whereHas('productOptions', function($q) { $q->whereHas('option', function($q) { $q->where('name', 'not like', '%Oversized Shipping%'); }); }) ->get();

这是我到目前为止所做的:

ProductOption

如何添加必须至少有两个有效 //calulating start date $date = new DateTime(date("Y-m-d")); $date->sub(new DateInterval('P1D')); //Adding Dimensions $params = array('dimensions' => 'ga:userType'); // requesting the data $data = $service->data_ga->get("ga:xxxxxxxx", $date->format('Y-m-d'), date("Y-m-d"), "ga:users,ga:sessions", $params ); ?><html> <?php echo $date->format('Y-m-d') . " - ".date("Y-m-d"). "\n";?> <table> <tr> <?php //Printing column headers foreach($data->getColumnHeaders() as $header){ print "<td>".$header['name']."</td>"; } ?> </tr> <?php //printing each row. foreach ($data->getRows() as $row) { print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>"; } //printing the total number of rows ?> <tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr> </table> </html> <?php ?> s?

的约束

1 个答案:

答案 0 :(得分:1)

whereHas方法takes additional arguments just for this

$products = Product::whereHas('productOptions', function ($q) {
    $q->whereHas('option', function ($q) {
        $q->where('name', 'not like', '%Oversized Shipping%');
    });
}, '>=', 2)->get();