Eloquent first where is the article

I am wondering how Laravel implements eloquent syntax, so the first where clause can be called statically with User::where()

User::where('id', 23)->where('email', $email)->first();

Do they public static function where()and apublic function where()

+4
source share
2 answers

The challenge wherein the Eloquent model involves a bit of magic that happens behind the scenes. First, take an example:

User::where(’name’, ‘Joe’)->first;

There is no static method wherethat exists in a class Modelthat extends a class User.

What happens is that the PHP magic method is called __callStatic, which then tries to call the method where.

public static function __callStatic($method, $parameters)
{
    $instance = new static;

    return call_user_func_array([$instance, $method], $parameters);
}

where, PHP- __call, Model.

public function __call($method, $parameters)
{
    if (in_array($method, ['increment', 'decrement'])) {
        return call_user_func_array([$this, $method], $parameters);
    }

    $query = $this->newQuery();

    return call_user_func_array([$query, $method], $parameters);
}

, , :

$query = $this->newQuery();

Eloquent , where.

, `` `User:: where()` ` :

Illuminate\Database\Eloquent\Builder::where()

Builder, , , where(), get(), first(), update() ..

Laracasts () , Eloquent , .

+4

, .

, Model, . Model 2 "" , __call() __callStatic()

__call() .

__callStatic() .

, Model use Illuminate\Database\Query\Builder as QueryBuilder;

Builder, , public function where()

, User::where, __callStatic('where', $parameters) Model.

, .

+1

Source: https://habr.com/ru/post/1612221/


All Articles