Hey all,
I'm out of ideas on how to debug my problem, I can't find what's wrong for the life of me. Any help, please?
I am trying to seed my app but all I get is a Call to undefined method App\Models\User::set() when the AddressSeeder is running (I don't use the set() method anywhere, so it must be related to something else). Only package I use for now is spatie/laravel-permission, but I don't reckon that's what causing the problem (disabling that seeder doesn't change anything).
In overall, I'm trying to seed my app with users (with different roles : admin, subscriber and cooperator), attach at least one address to a user, then a cooperation to that address. I cannot fathom how to seed addresses and attach it to an already existing user.
App\Models\User.php :
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Address;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'first_name',
'last_name',
'date_of_birth',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'date_of_birth' => 'datetime',
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
public function addresses(): HasMany
{
return $this->hasMany(Address::class);
}
}
App\Models\Address.php
<?php
namespace App\Models;
use App\Models\User;
use App\Models\Cooperation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Address extends Model
{
use HasFactory;
protected $fillable = [
'label',
'user_id',
'representative',
'company_name',
'address',
'address_2',
'zipcode',
'city',
'country',
'telephone',
'mobile',
'is_company',
'siret',
'number_of_employees',
'year_croec',
'year_crcc',
'college',
'tva_intracom',
'cooperated_at',
'subscribed_at',
];
protected $casts = [
'user_id' => User::class,
'cooperated_at' => 'datetime',
'subscribed_at' => 'datetime',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function cooperation(): HasOne
{
return $this->hasOne(Cooperation::class);
}
}
database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Database\Seeders\UsersSeeder;
use Database\Seeders\AddressSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call([
RolesAndPermissionsSeeder::class,
UsersSeeder::class,
AddressSeeder::class,
CooperationSeeder::class,
]);
}
}
database/seeders/RolesAndPermissionsSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Spatie\Permission\Models\Role;
class RolesAndPermissionsSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// Coop
$groupCoop = [
'cooperator-receipt', // reçu de coopération
'refund-cooperation', // remboursement de coopération
'access-events', // voir et s'abonner aux événements
];
$this->createPermissions($groupCoop);
// Adh
$groupAdh = [
'read-documentation', // accéder à la documentation
'access-news', // accéder aux actualités
'access-revue', // accéder à la revue
'access-models', // accéder aux modèles
'access-fiches', // accéder aux fiches
];
$this->createPermissions($groupAdh);
// Admin
$groupAdmin = [
'create-users', // créer des utilisateurs
'edit-users', // modifier des utilisateurs
'delete-users', // supprimer des utilisateurs
];
$this->createPermissions($groupAdmin);
$roleCooperateur = Role::updateOrCreate(['name' => 'Coopérateur']);
$roleAdherent = Role::updateOrCreate(['name' => 'Adhérent']);
$roleAdmin = Role::updateOrCreate(['name' => 'Administrateur']);
$roleCooperateur->givePermissionTo($groupCoop);
$roleAdherent->givePermissionTo($groupCoop, $groupAdh);
$roleAdmin->givePermissionTo($groupCoop, $groupAdh, $groupAdmin);
}
public function createPermissions(array $permissions): void
{
foreach ($permissions as $permission) {
Permission::updateOrCreate(['name' => $permission]);
}
}
}
database/seeders/UsersSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Address;
use App\Models\Cooperation;
use App\Models\Subscription;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// Admin
$admin = User::factory()->create([
'first_name' => 'Admin',
'last_name' => 'ECFS',
'email' => '[email protected]',
]);
// $admin->assignRole('Administrateur');
// Adhérent
$adherent = User::factory()->create([
'first_name' => 'Adhérent',
'last_name' => 'ECFS',
'email' => '[email protected]',
]);
// $adherent->assignRole('Adhérent');
// Coopérateur
$cooperateur = User::factory()->create([
'first_name' => 'Coopérateur',
'last_name' => 'ECFS',
'email' => '[email protected]',
]);
// $cooperateur->assignRole('Coopérateur');
User::factory(20)->create()->each(function (User $user) {
// $user->assignRole('Adhérent');
});
User::factory(20)->create()->each(function (User $user) {
// $user->assignRole('Coopérateur');
});
}
}
database/seeders/AddressSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Address;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class AddressSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$users = User::all();
foreach ($users as $user) {
$address = Address::factory()->create([
'user_id' => $user->id,
]);
$user->addresses()->save($address);
}
}
}
database/migrations/XXX_create_users_table.php
<?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('users', function (Blueprint $table) {
$table->id();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->datetime('date_of_birth')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
database/migrations/XXX_create_addresses_table.php
<?php
use App\Models\User;
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('addresses', function (Blueprint $table) {
$table->id();
$table->string('label')->nullable();
$table->foreignIdFor(User::class)->constrained()->cascadeOnDelete();
$table->string('representative')->nullable();
$table->string('company_name')->nullable();
$table->string('address')->nullable();
$table->string('address_2')->nullable();
$table->string('zipcode')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('telephone')->nullable();
$table->string('mobile')->nullable();
$table->boolean('is_company')->nullable();
$table->string('siret')->nullable();
$table->integer('number_of_employees')->nullable();
$table->string('year_croec')->nullable();
$table->string('year_crcc')->nullable();
$table->string('college')->nullable();
$table->string('tva_intracom')->nullable();
$table->timestamp('cooperated_at')->nullable();
$table->timestamp('subscribed_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('addresses');
}
};
database/migrations/XXX_create_cooperations_table.php
<?php
use App\Models\Address;
use App\Models\User;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('cooperations', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Address::class)->constrained()->cascadeOnDelete();
$table->string('cooperation_number')->nullable();
$table->timestamps();
$table->timestamp('validated_at')->nullable();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('cooperations');
}
};
Any help is very welcome...
Thanks by advance