The issue you're encountering is due to the naming convention used by the foreignIdFor method in your migration. By default, foreignIdFor will generate a column name based on the plural form of the related model's name, which is why you're seeing members_id instead of member_id.
To resolve this, you can explicitly specify the column name in your migration. Here's how you can modify your migration file:
return new class extends Migration {
public function up()
{
Schema::create('testimonials', function (Blueprint $table) {
$table->id();
$table->foreignId('member_id')->constrained('members');
$table->text('testimonial');
$table->dateTime('submitted');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('testimonials');
}
};
In this updated migration:
-
$table->foreignId('member_id')->constrained('members');explicitly sets the column name tomember_idand establishes a foreign key constraint to thememberstable.
After updating your migration file, you should run the following commands to refresh your database:
php artisan migrate:fresh
This will drop all tables and re-run all migrations, ensuring that the testimonials table is created with the correct column name.
Now, when you run App\Models\Testimonials::factory(5)->create() in Tinker, it should work without any issues related to the column name.