Assume you going to create a blog and there is a field. So you need to set the max string length in your posts table for the body column. So in this example, I will show you how to set the max string length in laravel migration. I will show you laravel migration set string max length. Laravel provides many methods to do that like mediumText() longText() and text() type.

Now assume you need a medium text column then you can set:

<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->mediumText('column');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

 

Now assume you need a simple text column then you can set:

<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->text('column');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

 

Now assume you do not know how much text are you going to post for a single blog post. Then you can use longText():

<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->longText('column');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

 

 Read also: Laravel Migration Increase Column Size Example

 

Conclusion

I have tried to discuss the clear concept of laravel migration string max length. Now we know laravel migration set string max length. Hope this how to set max string length in laravel migration tutorial will help you.