Skip to content

Commit a55400c

Browse files
author
Itamar Junior
committed
Enhance LaravelAppointments by adding polymorphic relationships, updating factories, and improving test coverage
1 parent 6582463 commit a55400c

16 files changed

+213
-58
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ After installing the package, publish the configuration file:
1515
```bash
1616
php artisan vendor:publish --provider="Codeitamarjr\LaravelAppointments\LaravelAppointmentServiceProvider" --tag=config
1717
php artisan vendor:publish --provider="Codeitamarjr\LaravelAppointments\LaravelAppointmentServiceProvider" --tag=migrations
18+
php artisan vendor:publish --tag=tests # Optional
1819
```
1920

2021
Run the migrations to create the necessary database tables:
2122

2223
```bash
2324
php artisan migrate
25+
php artisan db:seed --class=Codeitamarjr\\LaravelAppointments\\Database\\Seeders\\EventSeeder # Optional
2426
```
2527

2628
## Usage

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"Codeitamarjr\\LaravelAppointments\\": "src/"
3030
}
3131
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Tests\\": "tests/"
35+
}
36+
},
3237
"extra": {
3338
"laravel": {
3439
"providers": [

src/Database/Factories/AppointmentFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Codeitamarjr\LaravelAppointments\Models\Appointment;
66
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Codeitamarjr\LaravelAppointments\Models\Slot;
78

89

910
/**
10-
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Appointment>
11+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\Codeitamarjr\LaravelAppointments\Models\Appointment>
1112
*/
1213
class AppointmentFactory extends Factory
1314
{
@@ -22,7 +23,8 @@ public function definition(): array
2223
{
2324
return [
2425
'slot_id' => Slot::factory(),
25-
'user_id' => \App\Models\User::factory(), // Adjust based on your user model namespace
26+
'participant_id' => config('laravel-appointments.models.participant')::factory(),
27+
'participant_type' => config('laravel-appointments.relationships.participant', 'participant'),
2628
];
2729
}
2830
}

src/Database/Factories/SlotFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Codeitamarjr\LaravelAppointments\Database\Factories;
44

5+
use Carbon\Carbon;
56
use Codeitamarjr\LaravelAppointments\Models\Slot;
67
use Codeitamarjr\LaravelAppointments\Models\Event;
78
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -20,10 +21,12 @@ class SlotFactory extends Factory
2021
*/
2122
public function definition(): array
2223
{
24+
$startTime = $this->faker->dateTimeBetween('now', '+1 week');
25+
$endTime = Carbon::parse($startTime)->addMinutes($this->faker->numberBetween(15, 120));
2326
return [
2427
'event_id' => Event::factory(),
25-
'start_time' => $this->faker->dateTimeBetween('now', '+1 week'),
26-
'end_time' => $this->faker->dateTimeBetween('+1 week', '+2 weeks'),
28+
'start_time' => $startTime,
29+
'end_time' => $endTime,
2730
'attendees_limit' => $this->faker->randomElement([null, 5, 10]),
2831
];
2932
}

src/Database/Migrations/2024_12_18_133759_create_appointments_table.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ public function up(): void
1313
{
1414
Schema::create('appointments', function (Blueprint $table) {
1515
$table->id();
16-
$table->foreignId('slot_id')->constrained()->cascadeOnDelete();
17-
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
16+
$table->unsignedBigInteger('slot_id');
17+
$table->morphs('participant'); // Adds participant_id and participant_type
1818
$table->timestamps();
19+
20+
$table->foreign('slot_id')->references('id')->on('slots')->onDelete('cascade');
1921
});
2022
}
2123

src/Database/Seeders/EventSeeder.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77

88
class EventSeeder extends Seeder
99
{
10-
/**
11-
* Run the database seeds.
12-
*/
1310
public function run(): void
1411
{
15-
Event::factory(5)
16-
->hasSlots(3)
17-
->hasSlots(3, [
18-
'attendees_limit' => 5
19-
])->create();
12+
// Create 5 events, each with 3 slots
13+
Event::factory()
14+
->count(5)
15+
->hasSlots(3, ['attendees_limit' => 5])
16+
->create();
2017
}
2118
}

src/LaravelAppointmentServiceProvider.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,58 @@ public function boot()
1010
{
1111
// Publish Configuration
1212
$this->publishes([
13-
__DIR__.'/config/laravel-appointments.php' => config_path('laravel-appointments.php'),
13+
__DIR__ . '/config/laravel-appointments.php' => config_path('laravel-appointments.php'),
1414
], 'config');
1515

1616
// Publish Migrations
1717
$this->publishes([
18-
__DIR__.'/database/migrations/' => database_path('migrations'),
18+
__DIR__ . '/database/migrations/' => database_path('migrations'),
1919
], 'migrations');
2020

2121
// Publish Seeders
2222
$this->publishes([
23-
__DIR__.'/database/seeders/' => database_path('seeders'),
23+
__DIR__ . '/database/seeders/' => database_path('seeders'),
2424
], 'seeders');
2525

2626
// Publish Factories
2727
$this->publishes([
28-
__DIR__.'/database/factories/' => database_path('factories'),
28+
__DIR__ . '/database/factories/' => database_path('factories'),
2929
], 'factories');
3030

3131
// Publish Controllers
3232
$this->publishes([
33-
__DIR__.'/Http/Controllers/' => app_path('Http/Controllers'),
33+
__DIR__ . '/Http/Controllers/' => app_path('Http/Controllers'),
3434
], 'controllers');
3535

3636
// Publish Models
3737
$this->publishes([
38-
__DIR__.'/Http/Models/' => app_path('Models'),
38+
__DIR__ . '/Http/Models/' => app_path('Models'),
3939
], 'models');
4040

41+
// Publish Traits
42+
$this->publishes([
43+
__DIR__ . '/Traits/' => app_path('Traits'),
44+
], 'traits');
45+
46+
// Publish Tests
47+
$this->publishes([
48+
__DIR__ . '/../Tests/' => base_path('Tests'),
49+
], 'tests');
50+
4151
// Publish Notifications
4252
$this->publishes([
43-
__DIR__.'/../Notifications/' => app_path('Notifications'),
53+
__DIR__ . '/../Notifications/' => app_path('Notifications'),
4454
], 'notifications');
4555

4656
// Load Migrations
47-
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
57+
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
4858
}
4959

5060
public function register()
5161
{
5262
// Merge the configuration file
5363
$this->mergeConfigFrom(
54-
__DIR__.'/config/laravel-appointments.php',
64+
__DIR__ . '/config/laravel-appointments.php',
5565
'laravel-appointments'
5666
);
5767
}

src/Models/Appointment.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@ class Appointment extends Model
99
{
1010
use HasFactory;
1111

12-
protected $fillable = ['slot_id', 'user_id'];
12+
protected $fillable = [
13+
'slot_id',
14+
'participant_id',
15+
'participant_type' // "Owner" or "Participant"
16+
];
1317

14-
public function slot() : \Illuminate\Database\Eloquent\Relations\BelongsTo
18+
/**
19+
* Define the polymorphic relationship with the participant.
20+
*/
21+
public function participant()
1522
{
16-
return $this->belongsTo(config('laravel-appointments.models.slot'));
23+
return $this->morphTo(config('laravel-appointments.relationships.participant', 'participant'));
1724
}
1825

19-
public function user() : \Illuminate\Database\Eloquent\Relations\BelongsTo
26+
/**
27+
* The slot that this appointment belongs to.
28+
*/
29+
public function slot(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2030
{
21-
return $this->belongsTo(config('laravel-appointments.models.user'));
31+
return $this->belongsTo(config('laravel-appointments.models.slot'));
2232
}
2333
}

src/Models/Event.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Event extends Model
1818

1919
public function slots()
2020
{
21-
return $this->hasMany(config('laravel-appointments.models.slot'));
21+
return $this->hasMany(\Codeitamarjr\LaravelAppointments\Models\Slot::class);
2222
}
2323

2424
protected static function newFactory()

src/Traits/HasAppointments.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Codeitamarjr\LaravelAppointments\Traits;
4+
5+
use Codeitamarjr\LaravelAppointments\Models\Appointment;
6+
7+
trait HasAppointments
8+
{
9+
/**
10+
* Define the polymorphic relationship with appointments.
11+
*/
12+
public function appointments()
13+
{
14+
return $this->morphMany(Appointment::class, 'participant');
15+
}
16+
}

0 commit comments

Comments
 (0)