0
votes

It's more a "global understanding" question.

To save a model instance in the Database, we can use both:

SAVE()

$model = new Model;
$model->attribute = value;
$model->save();

https://laravel.com/docs/5.4/eloquent#inserts

and

::CREATE()

App\Model::create(['attribute'=>'value']);

https://laravel.com/docs/5.4/eloquent#mass-assignment

I supposed both of these methods belong to Illuminate\Database\Eloquent\Model, but I have found only function save there:

 public function save(array $options = [])
    {
        $query = $this->newQueryWithoutScopes();

     //......

        return $saved;
    }

But I haven't found any function Create in that file.

My QUESTIONS are:

1) what is the fundamental difference between

->method()

and

::method() 

(is the last one a query builder?)

2) where can I find "::create()" method declared?

Thank you very much!

2
In general, whenever you see ::method, that means there is a static method which can be called without generating any instance of the class. This is about OOP. Not specifically for Laravel - Mojtaba

2 Answers

3
votes
  1. ::method() is static calling without the need of creating an object of the class beforehand. ->method() you have to create an object before.

    $car = new Car();

    $car->color = 'red';

    $car->save();

vs.

$car = Car::create(['color' => 'red']);
  1. The create method can be found:

    \Illuminate\Database\Eloquent\Builder::create

1
votes

1)

->mehtod() is calling a Non-Static or Instantiated object method. Where as ::method() is calling on a static public method of a class.

To help describe this in your context. Take a look at how ::create() Operates. It returns an object that you can now use the save() method on after making changes. In the inverse, you cannot 'create' a model object from the save() method. You must have a model object first before executing ->save(). Which where ::create() comes in.

Eloquent ORM - Laravel : Insert, Update, Delete

2)

the create method is declared, I believe, in a higher level.