feat: add favorites feature (model, controller, routes, view)
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Favorite;
|
||||
use App\Models\Flight;
|
||||
use App\Models\Tour;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FavoriteController extends Controller
|
||||
{
|
||||
//
|
||||
public function index()
|
||||
{
|
||||
$favorites = auth()->user()->favorites()->with('favoritable')->get();
|
||||
|
||||
$items = $favorites->map(function ($favorite) {
|
||||
$item = $favorite->favoritable;
|
||||
|
||||
|
||||
|
||||
if ($item instanceof Flight) {
|
||||
return [
|
||||
'favorite_id' => $favorite->id,
|
||||
'type' => 'flight',
|
||||
'title' => 'پرواز ' . $item->flight_number,
|
||||
'subtitle' => ($item->originAirport->city ?? '') . ' به ' . ($item->destinationAirport->city ?? ''),
|
||||
'price' => number_format($item->price) . ' تومان',
|
||||
'favoritable_id' => $item->id,
|
||||
'favoritable_type' => 'flight',
|
||||
];
|
||||
}
|
||||
|
||||
if ($item instanceof Tour) {
|
||||
return [
|
||||
'favorite_id' => $favorite->id,
|
||||
'type' => 'tour',
|
||||
'title' => $item->title,
|
||||
'subtitle' => $item->summary,
|
||||
'price' => null,
|
||||
'favoritable_id' => $item->id,
|
||||
'favoritable_type' => 'tour',
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
})->filter()->values(); // حذف nullها و ریایندکس
|
||||
|
||||
return view('favorites.index', compact('items' , 'favorites'));
|
||||
}
|
||||
public function toggle($type , $id)
|
||||
{
|
||||
|
||||
|
||||
$modelClass = $type === 'flight' ? Flight::class : Tour::class;
|
||||
|
||||
$favorite = Favorite::where('user_id', auth()->id())
|
||||
->where('favoritable_id', $id)
|
||||
->where('favoritable_type', $modelClass)
|
||||
->first();
|
||||
|
||||
if ($favorite) {
|
||||
$favorite->delete();
|
||||
} else {
|
||||
Favorite::create([
|
||||
'user_id' => auth()->id(),
|
||||
'favoritable_id' => $id,
|
||||
'favoritable_type' => $modelClass,
|
||||
]);
|
||||
}
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class Favorite
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int $favoritable_id
|
||||
* @property string $favoritable_type
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
*
|
||||
* @property User $user
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class Favorite extends Model
|
||||
{
|
||||
protected $table = 'favorites';
|
||||
|
||||
protected $casts = [
|
||||
'user_id' => 'int',
|
||||
'favoritable_id' => 'int'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'favoritable_id',
|
||||
'favoritable_type'
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function favoritable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
||||
@@ -98,4 +98,8 @@ class Flight extends Model
|
||||
{
|
||||
return $this->belongsTo(Airport::class, 'destination_airports_id');
|
||||
}
|
||||
public function favorites()
|
||||
{
|
||||
return $this->morphMany(Favorite::class, 'favoritable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,4 +74,8 @@ class Tour extends Model
|
||||
{
|
||||
return $this->hasMany(TourDetail::class);
|
||||
}
|
||||
public function favorites()
|
||||
{
|
||||
return $this->morphMany(Favorite::class, 'favoritable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,4 +72,8 @@ class User extends Authenticated
|
||||
{
|
||||
return $this->belongsTo(File::class);
|
||||
}
|
||||
public function favorites()
|
||||
{
|
||||
return $this->hasMany(Favorite::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
@extends('dashboard.layouts.app')
|
||||
|
||||
@section('title' , 'علاقه مندی ها')
|
||||
|
||||
@section('account-content')
|
||||
|
||||
<div class="user-profile-wrapper">
|
||||
<div class="user-profile-card">
|
||||
<h4 class="user-profile-card-title">فهرست من</h4>
|
||||
|
||||
@if($items->isEmpty())
|
||||
<section class="flex flex-col items-center justify-center gap-y-3 mt-5">
|
||||
<h1 class="font-DanaDemiBold text-gray-700 dark:text-gray-300 text-2xl mt-2">
|
||||
در علاقه مندی شما چیزی وجود ندارد
|
||||
</h1>
|
||||
</section>
|
||||
@else
|
||||
<div class="row mt-30">
|
||||
@foreach($items as $item)
|
||||
<div class="col-md-6 col-lg-4">
|
||||
<div class="hotel-item">
|
||||
<div class="hotel-img">
|
||||
<img src="{{ $item['image'] ?? 'assets/img/hotel/01.jpg' }}" alt="{{ $item['title'] }}">
|
||||
<a href="{{ route('favorites.toggle', ['type' => $item['favoritable_type'], 'id' => $item['favoritable_id']]) }}"
|
||||
class="add-wishlist">
|
||||
<i class="fas fa-heart"></i>
|
||||
</a>
|
||||
</div>
|
||||
<div class="hotel-content">
|
||||
<h4 class="hotel-title"><a href="#">{{ $item['title'] }}</a></h4>
|
||||
<p><i class="far fa-location-dot"></i> {{ $item['subtitle'] }}</p>
|
||||
|
||||
<div class="hotel-bottom">
|
||||
@if($item['price'])
|
||||
<div class="hotel-price">
|
||||
<span class="hotel-price-amount">
|
||||
{{ $item['price'] }}
|
||||
</span>
|
||||
</div>
|
||||
@endif
|
||||
@if($item['favoritable_type'] == 'tour')
|
||||
<div class="hotel-text-btn">
|
||||
<a href="{{route('tour.show' , $item['favoritable_id'])}}">نمایش جزئیات <i class="fas fa-arrow-left"></i></a>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
+6
-1
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\FavoriteController;
|
||||
use App\Http\Controllers\FlightController;
|
||||
use App\Http\Controllers\IndexController;
|
||||
use App\Http\Controllers\TourController;
|
||||
@@ -14,5 +15,9 @@ Route::prefix('tour')->controller(TourController::class)->name('tour.')->group(f
|
||||
});
|
||||
Route::prefix('flight')->controller(FlightController::class)->name('flight.')->group(function (){
|
||||
Route::get('index' , 'index')->name('index');
|
||||
Route::get('show/{flight}' , 'show')->name('show');
|
||||
});
|
||||
|
||||
Route::middleware('auth')->group(function () {
|
||||
Route::get('toggle/{type}/{id}', [FavoriteController::class, 'toggle'])->name('favorites.toggle');
|
||||
Route::get('/favorites', [FavoriteController::class, 'index'])->name('favorites.index');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user