Since you have no test data in a fresh sql database we are going to fill it with fake data.
For that we will use the Factories in Laravel.
Writing a factory is like most things in Laravel a simple process.
By default the following factory is provided. The UserFactory:
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
}
So you define via $model = User::class the Eloquent model for which you create a factory.
Then in the definition function you pass in an array of values. Here we use the Faker library to generate some random property values.
Afterwards you need to check if you have imported the HasFactory trait into your model.
You can add it to the top of your class with:
use HasFactory;
The code snippet below is a summary of the most common functions you need to create Eloquent instances. We use the factory() method, with the count() or times() function we can create multiple models at once, we will get back a Collection of Eloquent models.
Note that through the make() function these are not saved to your database, you must use the create() method for this.
//Create a user, but don't save it yet
$user = User::factory()->make();
//Create multiple users, again we're not saving them yet.
$users = User::factory()->count(10)->make();
//Create a user and save them immediately
$user = User::factory()->create();