openclassify/Modules/Listing/Models/Listing.php
fatihalp 7e9d77c0a8 feat: Implement user profile management and favorites module
- Added routes for user profile management including edit, update, and delete functionalities.
- Created ProfileController to handle profile-related requests.
- Introduced Profile model to manage user profile data.
- Developed user status states (Active, Banned, Suspended) with appropriate labels and descriptions.
- Implemented favorite listings and sellers functionality in the User model.
- Created views for profile editing, updating password, and deleting account.
- Added migration for user and profile tables along with necessary fields.
- Registered User module with service provider and routes.
2026-03-05 01:23:42 +03:00

195 lines
5.8 KiB
PHP

<?php
namespace Modules\Listing\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Modules\Listing\States\ListingStatus;
use Modules\Listing\Support\ListingPanelHelper;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\ModelStates\HasStates;
class Listing extends Model implements HasMedia
{
use HasFactory, HasStates, InteractsWithMedia, LogsActivity;
protected $fillable = [
'title', 'description', 'price', 'currency', 'category_id',
'user_id', 'status', 'images', 'custom_fields', 'slug',
'contact_phone', 'contact_email', 'is_featured', 'expires_at',
'city', 'country', 'latitude', 'longitude', 'location', 'view_count',
];
protected $casts = [
'images' => 'array',
'custom_fields' => 'array',
'is_featured' => 'boolean',
'view_count' => 'integer',
'expires_at' => 'datetime',
'price' => 'decimal:2',
'latitude' => 'decimal:7',
'longitude' => 'decimal:7',
'status' => ListingStatus::class,
];
protected $appends = ['location'];
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logFillable()
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
public function category()
{
return $this->belongsTo(\Modules\Category\Models\Category::class);
}
public function user()
{
return $this->belongsTo(\Modules\User\App\Models\User::class);
}
public function favoritedByUsers()
{
return $this->belongsToMany(\Modules\User\App\Models\User::class, 'favorite_listings')
->withTimestamps();
}
public function conversations()
{
return $this->hasMany(\Modules\Conversation\App\Models\Conversation::class);
}
public function scopePublicFeed(Builder $query): Builder
{
return $query
->where('status', 'active')
->orderByDesc('is_featured')
->orderByDesc('created_at');
}
public function scopeSearchTerm(Builder $query, string $search): Builder
{
$search = trim($search);
if ($search === '') {
return $query;
}
return $query->where(function (Builder $searchQuery) use ($search): void {
$searchQuery
->where('title', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->orWhere('city', 'like', "%{$search}%")
->orWhere('country', 'like', "%{$search}%");
});
}
public function scopeForCategory(Builder $query, ?int $categoryId): Builder
{
if (! $categoryId) {
return $query;
}
return $query->where('category_id', $categoryId);
}
public function themeGallery(): array
{
$mediaUrls = $this->getMedia('listing-images')
->map(fn ($media): string => $media->getUrl())
->filter(fn (string $url): bool => $url !== '')
->values()
->all();
if ($mediaUrls !== []) {
return $mediaUrls;
}
return collect($this->images ?? [])
->filter(fn ($value): bool => is_string($value) && trim($value) !== '')
->values()
->all();
}
public function relatedSuggestions(int $limit = 8): Collection
{
$baseQuery = static::query()
->publicFeed()
->with('category:id,name')
->whereKeyNot($this->getKey());
$primary = (clone $baseQuery)
->forCategory($this->category_id ? (int) $this->category_id : null)
->limit($limit)
->get();
if ($primary->count() >= $limit) {
return $primary;
}
$missing = $limit - $primary->count();
$excludeIds = $primary->pluck('id')->push($this->getKey())->all();
$fallback = (clone $baseQuery)
->whereNotIn('id', $excludeIds)
->limit($missing)
->get();
return $primary->concat($fallback)->values();
}
public static function createFromFrontend(array $data, null | int | string $userId): self
{
$baseSlug = Str::slug((string) ($data['title'] ?? 'listing'));
$baseSlug = $baseSlug !== '' ? $baseSlug : 'listing';
do {
$slug = $baseSlug.'-'.Str::random(6);
} while (static::query()->where('slug', $slug)->exists());
$payload = $data;
$payload['user_id'] = $userId;
$payload['currency'] = ListingPanelHelper::normalizeCurrency($data['currency'] ?? null);
$payload['slug'] = $slug;
return static::query()->create($payload);
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('listing-images');
}
protected function location(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes): ?array {
$latitude = $attributes['latitude'] ?? null;
$longitude = $attributes['longitude'] ?? null;
if ($latitude === null || $longitude === null) {
return null;
}
return [
'lat' => (float) $latitude,
'lng' => (float) $longitude,
];
},
set: fn (?array $value): array => [
'latitude' => is_array($value) ? ($value['lat'] ?? null) : null,
'longitude' => is_array($value) ? ($value['lng'] ?? null) : null,
],
);
}
}