Skip to content

Commit

Permalink
Add relationship methods to Models
Browse files Browse the repository at this point in the history
Relationship methods were added to the Santo, User, and Segnalazione models. This change allows the application to retrieve related database entities more efficiently. The HasMany and BelongsTo relations were established according to the data structure.
  • Loading branch information
marco-introini committed Jun 1, 2024
1 parent 39e73b1 commit 4a193f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/Models/Santo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Santo extends Model
{
Expand Down Expand Up @@ -33,4 +34,10 @@ public function fonte(): BelongsTo
{
return $this->belongsTo(Fonte::class);
}

public function segnalazioni(): HasMany
{
return $this->hasMany(Segnalazione::class);
}

}
13 changes: 13 additions & 0 deletions app/Models/Segnalazione.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Models;

use AnourValar\EloquentSerialize\Tests\Models\User;
use App\Enums\TipoSegnalazione;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Segnalazione extends Model
{
Expand All @@ -15,4 +17,15 @@ class Segnalazione extends Model
protected $casts = [
'tipo_segnalazione' => TipoSegnalazione::class,
];

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

public function santo(): BelongsTo
{
return $this->belongsTo(Santo::class);
}

}
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Panel;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -70,4 +71,9 @@ public function getFilamentAvatarUrl(): ?string
{
return $this->avatar ? Storage::disk('avatars')->url($this->avatar) : null ;
}

public function segnalazioni(): HasMany
{
return $this->hasMany(Segnalazione::class, 'user_id');
}
}

0 comments on commit 4a193f8

Please sign in to comment.