Yes. A factory for user
You may have one already?
Then just do User::factory() for the user Id in the other factory.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Decided to add a factory for my basic Jetstream Inertia app running Docker Sail. But when I do run this factory I get this error
docker exec -it 7dbeb42826d3009b1b927a83dfaf9e62a2edf172c220e425fcc4ff3809096572 php artisan migrate:fresh --seed
Dropping all tables ............................................. 133ms DONE
INFO Preparing database.
Creating migration table ......................................... 29ms DONE
INFO Running migrations.
2014_10_12_000000_create_users_table ............................. 37ms DONE
2014_10_12_100000_create_password_resets_table ................... 25ms DONE
2014_10_12_200000_add_two_factor_columns_to_users_table .......... 12ms DONE
2019_08_19_000000_create_failed_jobs_table ....................... 41ms DONE
2019_12_14_000001_create_personal_access_tokens_table ............ 46ms DONE
2020_05_21_100000_create_teams_table ............................. 22ms DONE
2020_05_21_200000_create_team_user_table ......................... 25ms DONE
2020_05_21_300000_create_team_invitations_table .................. 53ms DONE
2021_07_12_030017_create_sessions_table .......................... 61ms DONE
2021_07_12_082029_create_articles_table .......................... 44ms DONE
INFO Seeding database.
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `articles` (`title`, `content`, `updated_at`, `created_at`) values (Architecto magnam velit sapiente quod velit eaque vitae molestias., Dolores et facere est. Minus magnam quasi dolor sit voluptatem omnis. Non rerum harum rerum amet sint assumenda., 2023-02-12 02:11:39, 2023-02-12 02:11:39))
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
756▕ // If an exception occurs when attempting to run a query, we'll format the error
757▕ // message to include the bindings with SQL, which will make this exception a
758▕ // lot more helpful to the developer instead of just the database's errors.
759▕ catch (Exception $e) {
➜ 760▕ throw new QueryException(
761▕ $query, $this->prepareBindings($bindings), $e
762▕ );
763▕ }
764▕ }
+16 vendor frames
17 database/seeders/DatabaseSeeder.php:16
Illuminate\Database\Eloquent\Factories\Factory::create()
+35 vendor frames
53 artisan:35
Illuminate\Foundation\Console\Kernel::handle()
Factory database/factories/ArticleFactory.php is
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
*/
class ArticleFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => $this->faker->sentence(6),
'content' => $this->faker->paragraph(4),
];
}
}
and included via database seeder:
<?php
namespace Database\Seeders;
use App\Models\Article;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
Article::factory(10)->create();
}
}
Article Model is
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'content',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
How can I add user ids properly? Perhaps I need to add a user factory first?
public function definition()
{
return [
'title' => $this->faker->sentence(6),
'content' => $this->faker->paragraph(4),
‘User_id’ => User::factory()
];
}
You can just run this factory. And it will create users for the discussions. Fix my syntax. On my phone.
Please or to participate in this conversation.