Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the make() method do in Laravel?

In the Laravel documentation, I found the following - https://laravel.com/docs/5.4/container#the-make-method

but I am still confused as to what exactly the make() method does. I know the create() method uses the make() method and then persists them into the database, so does make() methods just temporarily save it in php tinker or something? Sorry, I'm Laravel noob. I'm trying to figure out these functions. Thank you! :)

like image 300
Simon Suh Avatar asked May 22 '17 23:05

Simon Suh


People also ask

What is make () In Laravel?

The make method will return an instance of the class or interface you request. Where you request to make an interface, Laravel will lookup a binding for that interface to a concrete class. E.g. $app->make('App\Services\MyService'); // new \App\Services\MyService.

What is difference between create and save in Laravel?

Save can be used to both create a new Record and update a existing record . Whereas create is used to create a new record by providing all required field at one time .

What is fillable in Laravel model?

In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database.

What does => mean in Laravel?

-> and => are both operators. The difference is that => is the assign operator that is used while creating an array. For example: array(key => value, key2 => value2) And -> is the access operator. It accesses an object's value.


2 Answers

The make method will return an instance of the class or interface you request. Where you request to make an interface, Laravel will lookup a binding for that interface to a concrete class.

E.g.

$app->make('App\Services\MyService'); // new \App\Services\MyService.

One advantage to using the make method, is that Laravel will automatically inject any dependencies the class may define in it's constructor.

E.g. an instance of the Mailer class would be automatically injected here.

namespace App\Services;

use \Illuminate\Mail\Mailer;

class MyService
{
    public function __construct(Mailer $mailer) {
        $this->mailer = new Mailer;
    }
}
like image 114
fubar Avatar answered Oct 17 '22 05:10

fubar


I discovered recently that when you use make (), you are installing the class and you can access the methods of that class or model, this is a useful for the Test and validate that you are getting what you want Example: User model

class User extends Authenticatable
{
public function getRouteKeyName ()
     {
         return 'name';
     }
}

Test user

class UserTest extends TestCase
{
  public function route_key_name_is_set_to_name ()
     {
       $ user = factory (User :: class) -> make ();
       $ this-> assertEquals ('name', $ user-> getRouteKeyName ());
       // When you access the getRouteKeyName method you get the return, that is 'name'
     }
}

On the other hand if you use "create" that would give an error because you are creating a user

like image 28
Mau Web Avatar answered Oct 17 '22 05:10

Mau Web