- Add notes() morphMany relationship to Member model (ordered by created_at desc)
- Register morph map in AppServiceProvider ('member' => Member::class)
- Create NoteFactory with forMember() and byAuthor() state methods
45 lines
1015 B
PHP
45 lines
1015 B
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Member;
|
|
use App\Models\Note;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
class NoteFactory extends Factory
|
|
{
|
|
protected $model = Note::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
return [
|
|
'notable_type' => 'member', // Uses morph map alias
|
|
'notable_id' => Member::factory(),
|
|
'content' => $this->faker->paragraph(),
|
|
'author_user_id' => User::factory(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Set the note to belong to a specific member
|
|
*/
|
|
public function forMember(Member $member): static
|
|
{
|
|
return $this->state(fn () => [
|
|
'notable_type' => 'member',
|
|
'notable_id' => $member->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Set the note to be authored by a specific user
|
|
*/
|
|
public function byAuthor(User $user): static
|
|
{
|
|
return $this->state(fn () => [
|
|
'author_user_id' => $user->id,
|
|
]);
|
|
}
|
|
}
|