first commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Attraction extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $fillable = [
|
||||
'city_id',
|
||||
'name',
|
||||
'description',
|
||||
'latitude',
|
||||
'longitude',
|
||||
'category',
|
||||
'source',
|
||||
'source_id',
|
||||
'rating',
|
||||
'image_url',
|
||||
'cached_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'latitude' => 'decimal:7',
|
||||
'longitude' => 'decimal:7',
|
||||
'rating' => 'decimal:2',
|
||||
'cached_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the city that owns this attraction
|
||||
*/
|
||||
public function city(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(City::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class City extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'country',
|
||||
'latitude',
|
||||
'longitude',
|
||||
'wikipedia_summary',
|
||||
'wikipedia_url',
|
||||
'thumbnail_url',
|
||||
'image_url',
|
||||
'wikipedia_page_id',
|
||||
'wikipedia_cached_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'latitude' => 'decimal:7',
|
||||
'longitude' => 'decimal:7',
|
||||
'wikipedia_cached_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the route key for the model.
|
||||
*/
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot the model and set up event listeners
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($city) {
|
||||
if (empty($city->slug)) {
|
||||
$city->slug = static::generateUniqueSlug($city->name);
|
||||
}
|
||||
});
|
||||
|
||||
static::updating(function ($city) {
|
||||
if ($city->isDirty('name') && empty($city->slug)) {
|
||||
$city->slug = static::generateUniqueSlug($city->name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique slug from city name
|
||||
*/
|
||||
protected static function generateUniqueSlug(string $name): string
|
||||
{
|
||||
$slug = Str::slug($name);
|
||||
$originalSlug = $slug;
|
||||
$count = 1;
|
||||
|
||||
while (static::where('slug', $slug)->exists()) {
|
||||
$slug = $originalSlug . '-' . $count;
|
||||
$count++;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all attractions for this city
|
||||
*/
|
||||
public function attractions(): HasMany
|
||||
{
|
||||
return $this->hasMany(Attraction::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all trips that include this city
|
||||
*/
|
||||
public function trips(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Trip::class, 'city_trip')
|
||||
->withPivot('order')
|
||||
->withTimestamps()
|
||||
->orderByPivot('order');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get users who favorited this city
|
||||
*/
|
||||
public function favoritedBy(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'favorites')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Favorite extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'city_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the user that owns this favorite
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the city that was favorited
|
||||
*/
|
||||
public function city(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(City::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Trip extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'name',
|
||||
'description',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the user that owns this trip
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all cities in this trip
|
||||
*/
|
||||
public function cities(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(City::class, 'city_trip')
|
||||
->withPivot('order')
|
||||
->withTimestamps()
|
||||
->orderByPivot('order');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable, HasApiTokens;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all favorites for this user
|
||||
*/
|
||||
public function favorites(): HasMany
|
||||
{
|
||||
return $this->hasMany(Favorite::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all favorite cities for this user
|
||||
*/
|
||||
public function favoriteCities(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(City::class, 'favorites')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all trips for this user
|
||||
*/
|
||||
public function trips(): HasMany
|
||||
{
|
||||
return $this->hasMany(Trip::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user