在Eloquent中覆盖自定义newQuery()
问题描述:
For my Deal model, I'm using newQuery()
to ensure that only unexpired and approved deals are ever queried. However, a few cases have come up where I don't want these two conditions to be in effect, and I want to query all deals.
What can I do?
public function newQuery($excludeDeleted = true) {
return parent::newQuery()->where('end_date', '>', date('Y-m-d H:i:s'))->where('approved', 1);
}
Note: I'm not using Laravel, I'm just using the Eloquent library via Composer.
答
You are probably looking for conditional clauses - https://laravel.com/docs/5.4/queries#conditional-clauses
and some examples: