You must disable timestamps on your User model as shown here in the documentation.
SQLSTATE[42S22]: Column not found: 1054
thats my UserFactory
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class UserFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'login' => fake()->name(),
'pass' => Hash::make(Str::random(15)),
'cargo' => 1
];
}
}
and the migration file
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('usuario', function (Blueprint $table) {
$table->id('usuarioid');
$table->string('login')->unique();
$table->string('pass');
$table->foreignId('cargo');
});
Schema::create('cargo', function (Blueprint $table) {
$table->id('cargoid');
$table->string('cargo')->unique();
$table->string('sigla')->unique();
});
Schema::create('objeto', function (Blueprint $table) {
$table->id('objetoid');
$table->string('related_to');
$table->integer('related_id')->nullable(false);
$table->timestamps();
});
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->longText('payload');
$table->integer('last_activity')->index();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('usuario');
Schema::dropIfExists('cargo');
Schema::dropIfExists('objeto');
Schema::dropIfExists('sessions');
}
};
and the seeder
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
User::factory(10)->create();
for($x = 1; $x < 11; $x++){
DB::table('objeto')->insert([
'related_to' => 'USUARIO',
'related_id' => $x,
'created_at' => now(),
'updated_at' => now()
]);
}
DB::table('cargo')->insert([
'cargo' => 'Usuario Comum',
'sigla' => 'C1',
]);
}
}
and this is the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (Connection: mysql, SQL: insert into `usuario` (`login`, `pass`, `cargo`, `updated_at`, `created_at`) values (Judge Boyle I, $2y$12$wjh5yl2Rw9VVc9TokGlH5uFXulI2JJzJM4UJXbPDmjdp6yY.aVzR6, 1, 2025-03-05 02:29:37, 2025-03-05 02:29:37))
the problem is: i used to have those fields updated_at and created_at on my 'usuario' table, but i took them out of the code, but for some reason my seeder still expects that these two tables are still on the database
You need to add the timestamps to this migration.
Schema::create('usuario', function (Blueprint $table) {
$table->id('usuarioid');
$table->string('login')->unique();
$table->string('pass');
$table->foreignId('cargo');
});
It should be
Schema::create('usuario', function (Blueprint $table) {
$table->id('usuarioid');
$table->string('login')->unique();
$table->string('pass');
$table->foreignId('cargo');
$table->timestamps();
});
However there are a lot of problems with your database model.
It does not follow the conventions of Laravel.
- id/primary key columns should be named id
- foreign key columns should be named <model they are referencing>_id, like user_id
- use english to name tables and fields
- Table names should be in plural form
Then of course, don't roll your own authentication, use a starter kit for that.
Oh and give these a read or three.
https://tray2.se/posts/database-design
https://tray2.se/posts/database-design-part-2
https://tray2.se/posts/sqlerrm
https://tray2.se/posts/properly-formed-foreign-keys-are-your-best-friends
Please or to participate in this conversation.