Post table seeder laravel 10
Building your Laravel application can be an exciting journey, but starting with an empty database isn't always ideal. This is where seeding comes in, allowing you to inject pre-defined data and see your application come to life. Today, we'll focus on seeding the Post table, using both factories and the seeder class. Get ready to unleash a vibrant world of dummy data!
Step 1: Crafting the Post Factory:
-
Open your terminal: Navigate to your Laravel project directory using the
cd
command. -
Generate the Factory: Run
php artisan make:factory PostFactory
. This creates a file namedPostFactory.php
within thedatabase/factories
directory. -
Define the Dummy Data: Open the
PostFactory.php
file and edit the definition method. Here, you can specify the fields you want to populate with dummy data. Let's add some essentials like title, body, user_id, is_published, created_at and updated_at:
Here is the PostFactory.php code :
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Model>
*/
class PostFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => $this->faker->sentence,
'body' => $this->faker->paragraph(20, 40),
'is_published' => $this->faker->randomElement(['0', '1']),
'created_at' => now(),
'updated_at' => now(),
'user_id' => \App\Models\User::all()->random()->id
];
}
}
Step 2: Building the Post Seeder:
-
Generate the Seeder: Run
php artisan make:seeder PostTableSeeder
. This creates a file namedPostTableSeeder.php
within thedatabase/seeders
directory. -
Populate the Database: Open the
PostTableSeeder.php
file and add the following code:
public function run(): void
{
\App\Models\Post::factory()->count(5)->create();
}
Step 3: Seeding in Action:
- Now open
database\seeders\DatabaseSeeder.php
and place below code in it.
public function run(): void
{
// \App\Models\User::factory(10)->create();
$this->call([
UserTableSeeder::class,
PostTableSeeder::class
]);
}
-
Run the Seeder: In your terminal, execute
php artisan db:seed
. This triggers the PostTableSeeder and injects your dummy data into the database.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Wheredate in laravel not working
- Display message with session flash using bootstrap alert class in laravel
- Check if Relationship Method Exists in Laravel
- Composer\Exception\NoSslException
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- Use withCount() to get total number of records with relationship
- How to pass data to route in laravel?
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Include External CSS and JS file in Laravel
- How to get list of all views file in laravel
- How to insert ckeditor data into database in Laravel?
- How to return error message from controller to view in laravel
- How to randomly get the user id from users table in laravel
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to pass query string with pagination in laravel
- How to send email in laravel
- Laravel file size validation not working
- Redirect to another view from controller in laravel
- Call to a member function pluck() on null
- First and last item of the array using foreach iteration in laravel blade
- If condition in foreach loop in laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- How to upload files to amazon s3 bucket using Laravel
- How to send ID to another page in Laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package