[英]Where clause in querybuilder and eloquent
我在下面看到了一些类似这样的代码:
public function scopeUserId($query, $user_id)
{
return $query->whereUserId($user_id);
}
现在,问题是UserId与哪里有关(尽管足够冗长,但是语法让我感到困惑),在laravel文档中哪里可以找到它?
好吧,这可能只是另一个(名字不佳)范围:
public function scopeWhereUserId($query, $user_id) {
return $query->where("user_id", $user_id);
}
public function scopeUserId($query, $user_id) {
return $query->whereUserId($user_id);
}
...
$roles = Roles::whereUserId($id);
$roles_flattened = Roles::userId($id)->flatten();
...或者是查询构建器的某种神奇扩展(或者是内置构建器中的?):
class MyQueryBuilder extends QueryBuilder {
...
public function __call($method, $args) {
if (Str::beginsWith($method, "where")) {
$whereColumn = str_replace("where", "", $method);
$whereColumn = camelCaseToSnakeCase($whereColumn);
return $this->where($whereColumn, array_shift($args))
}
return parent::__call($method, $args)
}
...
}
Upd:的确如此。 以下是相关的Illuminate\\Database\\Query\\Builder
类代码:
/**
* Handle dynamic method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
if (Str::startsWith($method, 'where')) {
return $this->dynamicWhere($method, $parameters);
}
$className = get_class($this);
throw new BadMethodCallException("Call to undefined method {$className}::{$method}()");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.