Reputation: 757
I've seen some code like this one below:
public function scopeUserId($query, $user_id)
{
return $query->whereUserId($user_id);
}
Now, the question is what is whereUserId pertain(though it is verbose enough, but the syntax gets me confused) and where can I find it in the laravel documentation?
Upvotes: 1
Views: 509
Reputation: 2568
Well, that can be just another (bad-named) scope:
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();
... or be some magic-extended substitution for query builder (or is it in built-in builder?):
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: Indeed it is. Here is related Illuminate\Database\Query\Builder
class code:
/**
* 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}()");
}
Upvotes: 2