本文整理汇总了PHP中Illuminate\Database\Eloquent\Builder::whereHas方法的典型用法代码示例。如果您正苦于以下问题:PHP Builder::whereHas方法的具体用法?PHP Builder::whereHas怎么用?PHP Builder::whereHas使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Builder
的用法示例。
在下文中一共展示了Builder::whereHas方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: scopeWithTag
public static function scopeWithTag(Builder $query, $tags, $type = 'slug')
{
$tags = (new static())->prepareTags($tags);
return $query->whereHas('tags', function ($query) use($type, $tags) {
$query->whereIn($type, $tags);
});
}
示例2: scopeVisible
/**
* @param Builder|\Illuminate\Database\Eloquent\Builder|Model $query
* @return mixed
*/
public function scopeVisible($query)
{
return $query->whereHas('profile', function ($query) {
/** @var Profile $query */
return $query->visible();
});
}
示例3: scopeHasActiveDiary
/**
* アクティブな日誌があるか
* @param Builder $query
* @param $cropId
* @return Builder|static
*/
public function scopeHasActiveDiary(Builder $query, $cropId = null)
{
return $query->whereHas('workDiaries', function ($query) use($cropId) {
$query->where('archive', false);
if (!is_null($cropId)) {
$query->where('crop_id', $cropId);
}
});
}
示例4: whereHas
/**
* 关联查询
* @param $relation
* @param \Closure $callback 由于此处 Closure 接受的参数是 Table 类,所以下面调用时封装了一次
* @return static
*/
public function whereHas($relation, $callback)
{
$this->original->whereHas($relation, function ($query) use($callback) {
call_user_func($callback, lego_table($query));
});
return $this;
}
示例5: scopeFilter
/**
* query filter with id scope
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFilter($query, $request)
{
// filter id
if ($request->has('id')) {
$query->where('id', $request->get('id'));
}
// filter title
if ($request->has('title')) {
$query->where('title', 'like', "%{$request->get('title')}%");
}
// filter category
if ($request->has('category')) {
$query->whereHas('category', function ($query) use($request) {
$query->where('name', 'like', "%{$request->get('category')}%");
});
}
// filter status
if ($request->has('status')) {
$query->where('is_publish', $request->get('status'));
}
// filter created_at
if ($request->has('created_at_from')) {
$query->where('created_at', '>=', Carbon::parse($request->get('created_at_from')));
}
if ($request->has('created_at_to')) {
$query->where('created_at', '<=', Carbon::parse($request->get('created_at_to')));
}
return $query;
}
示例6: constrainWhereIsAll
/**
* Constrain the given users query by all provided roles.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $role
* @return \Illuminate\Database\Eloquent\Builder
*/
public function constrainWhereIsAll($query, $role)
{
$roles = array_slice(func_get_args(), 1);
return $query->whereHas('roles', function ($query) use($roles) {
$query->whereIn('name', $roles);
}, '=', count($roles));
}
示例7: apply
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
*/
public function apply(Builder $builder, Model $model)
{
$builder->whereHas('meta', function ($query) {
$query->where('meta_key', '_apiposts_enable_api_key')->where('meta_value', 'yes');
self::$apiEnabledScopeColumn = $query->toSql();
});
}
示例8: scopeFormed
/**
* Scope a query to only include guilds that are fully formed.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFormed(Builder $query)
{
return $query->whereHas('memberships', function ($membership) {
$membership->whereHas('rank', function ($rank) {
$rank->where('level', '>', 1);
});
}, '>=', 4);
}
示例9: apply
/**
* Apply scope on the query.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$column = $model->getApplicationForeignKey(true);
$builder->whereHas('applications', function ($q) use($column) {
$q->where($column, CurrentApplication::get()->id);
});
$this->addAllApplications($builder);
}
示例10: apply
/**
* @param Builder $builder
* @param Model $model
*/
public function apply(Builder $builder, Model $model)
{
if (!Auth::guest() && Auth::user()->hasCountryRole()) {
$country = Auth::user()->country;
if ($builder->getModel()->getTable() == "activity_logs") {
$builder->whereHas('contract', function ($q) use($country) {
$q->whereRaw("contracts.metadata->'country'->>'code' in (?)", $country);
});
} elseif ($builder->getModel()->getTable() == "contract_annotations") {
$builder->whereHas('contract', function ($q) use($country) {
$q->whereRaw("contracts.metadata->'country'->>'code' in (?)", $country);
});
} else {
$builder->whereRaw("contracts.metadata->'country'->>'code' in (?)", $country);
}
}
}
示例11: apply
public function apply(Builder $builder, Model $model)
{
$account = Account::getCurrent();
if (isset($account)) {
$builder->whereHas('account', function ($query) use($account) {
$query->where('accounts.id', $account->id);
});
}
}
示例12: deleteByCategory
/**
* Delete All notifications from a
* defined category
*
* @param $category_name int
* @param $expired Bool
* @return Bool
*/
public function deleteByCategory($category_name, $expired = false)
{
$query = $this->notification->whereHas('body', function ($q) use($category_name) {
$q->where('name', $category_name);
});
if ($expired == true) {
return $query->onlyExpired()->delete();
}
return $query->delete();
}
示例13: apply
/**
* Applies constraint to query.
*
* @param Builder $builder query builder
* @param string $field field name
* @param string $mode determines how constraint is added to existing query ("or" or "and")
*/
public function apply(Builder $builder, $field, $mode = Constraint::MODE_AND)
{
if ($this->isRelation($field)) {
list($relation, $field) = $this->splitRelationField($field);
$builder->whereHas($relation, function (Builder $builder) use($field, $mode) {
$this->doApply($builder, $field, $mode);
});
} else {
$this->doApply($builder, $field, $mode);
}
}
示例14: queryRelations
/**
* @param Builder $query
* @param $column
* @param $value
*/
private function queryRelations(Builder $query, $column, $value)
{
list($relation_name, $column) = explode('.', $column, 2);
if (!is_array($value)) {
$query->whereHas($relation_name, function ($subQuery) use($column, $value) {
$subQuery->where($column, 'like', "%{$value}%");
});
} else {
//TODO: debatable
}
}
示例15: scopeFilter
/**
* Restrict the resource to the given filters.
* These are the main functionalities:
*
* - Scope on the (single) relation!
* Use "-" as delimeter between the resource name and attribute.
* > /v1/items?article:drug_amount=1000mg
*
* - Use different operators!
* Use one of the operators as first character in the value.
* > /v1/items?article:cost=]5
* > /v1/items?article:cost=[20
* > /v1/items?article:name=~panadol
*
* - It now uses byCedric/Inquiry package!
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $values (default: array())
* @param string $relation_delimeter (default: -)
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFilter($query, array $values = array())
{
// all allowed attributes
$attributes = array_keys($this->getArrayableItems(array_merge($this->attributes, array_flip($this->visible))));
// itterate over the provided queries
foreach ($values as $key => $value) {
// set some main variables
$inquiry = Inquiry::get($key);
$operator = null;
$relation = null;
$method = null;
// check if a valid operator was supplied
if (!$inquiry->hasOperator() && !$inquiry->hasRange()) {
continue;
}
// check if the key is a filterable attribute
if (!in_array($inquiry->getKey(), $attributes)) {
// if it doesn't has a relation, it's definitly not the good attribute
if (!$inquiry->swap()->hasRelation()) {
continue;
}
// get the relation, with the key (func:attr) as main value
$relation = $inquiry->swap()->getRelation();
$method = camel_case($relation->getRelated());
// check if the relation is allowed
if (!method_exists($this, $method) || !in_array($method, $attributes)) {
continue;
}
}
// check if the operator is a range query
if ($inquiry->hasRange()) {
// get the range values
$operator = $inquiry->getRange();
} else {
// get the operator query
$operator = $inquiry->getOperator();
}
// if a relation was provided
if ($relation !== null) {
// apply relation query
$query->whereHas($method, function ($query) use($operator, $relation) {
// apply final query
$this->applyQuery($query, $relation->getValue(), $operator);
});
// query already applied
continue;
}
// apply query
$this->applyQuery($query, $inquiry->getKey(), $operator);
}
// return the query for method chaining
return $query;
}