Implementation of github project Performance 3 out of memory - Part-1

Step 01: create fresh laravel project

composer create-project laravel/laravel performance3

Step 02: Setting Up Post model, migration and factory and Comment model migration, factory, seeder

php artisan make:model Post -mf
php artisan make:model Comment -mfs

Step 03
: Specifying migration definition in database\migrations\2022_07_31_183018_create_posts_table.php

Schema::create('posts', function (Blueprint $table) {
    $table->id();
$table->foreignId('user_id')->constrained();
$table->string('title');
$table->text('post_text');
$table->timestamps(); });


Then, specifying definition for database\migrations\2022_08_01_145902_create_comments_table.php

Schema::create('comments', function (Blueprint $table) {
   $table->id();
$table->foreignId('user_id')->constrained();
$table->foreignId('post_id')->constrained();
$table->text('comment_text');
$table->timestamps(); });


Step 04
: Specifying default length 200 in AppServiceProvider.php with Schema facade under boot() method to avoid mysql string length too long error while executing migration command.

Schema::defaultStringLength(200);


Step 05: Defining database name in .env file

Step 06: Specifying data definition in PostFactory.php

use App\Models\Post;

protected $model = Post::class;

public
function definition() { return [ 'title' => $this->faker->text(50), 'post_text' => $this->faker->text(500), ]; }


Then, specifying CommentFactory.php

use App\Models\Comment;

protected $model = Comment::class;

public
function definition() { return [ 'user_id' => rand(1,100),
'post_id' => rand(1,10000),
'comment_text' => $this->faker->text(500), ]; }


Step 07
: Creating UserSeeder.php using artisan command

php artisan make:seeder UserSeeder

Step 08: Specifying definition of UserSeeder.php

public function run()
{
	User::factory(100)
	->has(Post::factory(100))
	->create();
}


Then, specifying CommentSeeder

public function run()
{
   Comment::factory(50000)->create();
}

Step 09
: Running UserSeeder and CommentSeeder using run() method in DatabaseSeeder.php,  to run migrations for user, post and comment table. Relation is 1. user hasMany posts 2. post belongsTo user 3. Comment belongsTo user and post 4. User hasMany comments 5. Post hasMany comments

// DatabaseSeeder.php

$this->call([ UserSeeder::class,
CommentSeeder::class, ]);
//In User.php 

public function posts()
{
return $this->hasMany(Post::class);
}

public function comments()
{
return $this->hasMany(Comment::class);
}
//In Post.php public function user() { return $this->belongsTo(User::class); }


Step 10
: Installing laravel debugbar to check performance for eloquent queries

composer require barryvdh/laravel-debugbar

Step 11: Finally running following artisan command to run migration and seeding database tables. In Users table there will be 100 users created and in Posts table there will be 100 posts for each user. So in total there will be 10000 posts. Lastly, for comments table there will be 50000 comments.

php artisan migrate:fresh --seed

To continue read on next part click here

Related Posts