Laravel and Database OOP

Schema::create('activation_codes', function (Blueprint $table) {
    $table->id();
    $table->integer('user_id')->unsigned()->index();
    $table->string('code');
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

This is a schema from a Laravel file. While I understand most of the things, but I have a few questions:

  1. What does unsigned() part does here?
  2. I am also not sure how should I grasp this concept → $table->string('code'); There are various kinds of string type. What is it exactly doing?
  3. I find this part also very complicated → $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
  1. That means id >= 0.

  2. table.user_id is foreign key, referenced to primary key of another table users.id. That means users related to table as one-to-many (1:N). And by deleting of some record from users all related records from table will be also deleted.

Also to answer your question about the string, it is simply creating a varchar in the database. I believe the default is around 250 in length, but you can also specify a length as a second parameter.

This differs from other types which all have their own methods $this->longtext(), $this->text() etc.

If you would like to change the default size, you can do so by going into the app service provider…

You may configure the default string length by calling the Schema::defaultStringLength method within the boot method of your App\Providers\AppServiceProvider class

I hope this helps. :slight_smile:

1 Like

Yes, it did. so unless classified the default string type is varchar.

It should be yes.

1 Like

I also find this to be a useful article →

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.