Search code examples
phpmysqlooplaravelstatic-functions

What is this code meaning...?


I want to put this question for me. To better understand PHP and Laravel.

public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

This is actually a method with a static function inside it? And inside of this static function another one...? I have to say i am a beginner with Frameworks.

This is my first time playing around with one.


Solution

  • If your confusion is more about how the code works rather than what it does, it's actually a pretty complex subject. First, it's not really a static method.

    Laravel uses something call Facades that allows you to call methods "statically", which can be confusing for people new to the framework. In a nutshell, facades are a great convenience because when you call a method using Laravel's facade, it "masks" a ton of instance objects for you and then calls the method.

    Lets take for example Schema in this instance. Where is this Schema class? If you look inside your config/app.php file, you'll see this line of code under aliases:

    'Schema'    => Illuminate\Support\Facades\Schema::class,
    

    Schema is therefore an alias for Illuminate\Support\Facades\Schema.

    So if you take a look at Illuminate\Support\Facades\Schema, you'll see that it extends Facade. It is one of Laravel's facades. This directory has a ton of Laravel's facades. Anyway, at the top of this class, you'll also see a little convenient comment that says:

    @see \Illuminate\Database\Schema\Builder
    

    Open up that file, and this is your Schema class. Behind the scenes, Laravel instantiates this object for you. Why is this such a big deal? Take a look at the constructor. One of this class' dependencies is Illuminate\Database\Connection. Open up that class, and it has even more dependencies. Rather than having to manually instantiate all these classes all the time, facades do the work for you. So when you do Schema::create(..., Laravel is instantiating all these dependencies and calling the create method in the Illuminate\Database\Schema\Builder class. Pretty awesome, right? If you look in that file, you'll see the method:

    /**
     * Create a new table on the schema.
     *
     * @param  string    $table
     * @param  \Closure  $callback
     * @return \Illuminate\Database\Schema\Blueprint
     */
    public function create($table, Closure $callback)
    {
        $blueprint = $this->createBlueprint($table);
    
        $blueprint->create();
    
        $callback($blueprint);
    
        $this->build($blueprint);
    }
    

    You can see that it's not a static method. Laravel is creating a new instance behind the scenes and then calling the method.

    The first argument accepts a string, which would be the name of the table you are creating. Then it accepts a closure. In this case, as @Kostas Mitsarakis said, you are creating a table that is called tasks. This table has an auto-incrementing column called id, etc.