querybuilder 和 eloquent 中的 Where 子句

Where clause in querybuilder and eloquent

我在下面看到了一些类似这样的代码:

public function scopeUserId($query, $user_id)
{
   return $query->whereUserId($user_id);
}

现在,问题是 whereUserId 属于什么(尽管它足够冗长,但语法让我感到困惑)以及我在 laravel 文档中的什么地方可以找到它?

好吧,这可能只是另一个 (bad-named) 范围:

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();

... 或者 magic-extended 替代查询生成器(或者它在 built-in 生成器中?):

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)
}

...
}

更新:确实如此。这里是相关的Illuminate\Database\Query\Builderclass代码:

/**
 * 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}()");
}