Understanding the static method in the laravel model

I just went through the laravel online survey and I saw the following modal code as shown below:

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Notice extends Model {

    protected $fillable = [
        'provider_id',
        'infringing_title',    
        'infringing_link',    
        'original_link',    
        'original_description',    
        'template',    
        'content_removed'
    ];


    public static function open(array $attributes) {
        return new static($attributes); 
    } 

    public function useTemplate($template) {
        $this->template = $template;
    }

}

What I'm interested in knowing is how exactly the method described below is used:

public static function open(array $attributes) {
            return new static($attributes); 
} 

I understand its static method, but this line return new static($attributes);especially bothers me.

I see that the method is used as follows:

    $notice = Notice::open($date);

But I still do not quite understand its use. can someone explain.

+4
source share
2 answers

the static method can be used without instantiating the class, thus ::

return new static($attributes); creates a new model object from this class

which basically coincides with

$notice = new Notice;
$notice->provider_id = $provider_id;
...

, insatance, - $notice->save()

+6

. -, , new , .

- , .

Btw. mod e l, mod a l.

+2

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


All Articles