git commit "add favorites display with polymorphic relation"

This commit is contained in:
2026-08-17 14:14:55 +03:30
parent 902384625b
commit ea1a0cc0c4
6 changed files with 61 additions and 73 deletions
+8 -14
View File
@@ -9,30 +9,24 @@ class FavoriteController extends Controller
{
public function index()
{
$favorites=Favorite::with('tour')
->where('user_id',auth()->id())
$favorites = Favorite::with('favoritable')
->where('user_id', auth()->id())
->latest()
->get();
return view('account.users.favorite.index',compact('favorites'));
return view('account.users.favorite.index', compact('favorites'));
}
public function store(Request $request)
{
$request->validate([
'tour_id'=>[
'required',
'exists:tours,id'
],
]);
Favorite::firstOrCreate([
'user_id' => auth()->id(),
'tour_id' => $request->tour_id,
'favoritable_type' => $request->type,
'favoritable_id' => $request->id,
]);
return back()->with(
'success',
'تور به علاقه‌مندی‌ها اضافه شد.'
);
return back();
}
+6 -1
View File
@@ -7,7 +7,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model;
/**
* Class Favorite
@@ -43,4 +43,9 @@ class Favorite extends Model
{
return $this->belongsTo(User::class);
}
public function favoritable()
{
return $this->morphTo();
}
}
+4
View File
@@ -72,6 +72,10 @@ class Flight extends Model
{
return $this->hasMany(Comment::class);
}
public function favorites()
{
return $this->morphMany(Favorite::class, 'favoritable');
}
#[Scope]
public function applySearch(Builder $query, array $filters): void
+4
View File
@@ -62,4 +62,8 @@ class Tour extends Model
{
return $this->belongsTo(TravelType::class);
}
public function favorites()
{
return $this->morphMany(Favorite::class, 'favoritable');
}
}
@@ -93,74 +93,40 @@
@foreach($favorites as $favorite)
<div class="col-md-6">
@if($favorite->favoritable)
<div class="theme-search-results-item">
@if($favorite->favoritable_type == 'App\Models\Tour')
<div class="theme-search-results-item-img-wrap">
@if($favorite->tour->image)
<img
class="theme-search-results-item-img"
src="{{ asset('uploads/tours/' . $favorite->tour->image) }}"
alt="{{ $favorite->tour->title }}"
>
@else
<img
class="theme-search-results-item-img"
src="{{ asset('img/placeholder.jpg') }}"
alt="{{ $favorite->tour->title }}"
>
@endif
</div>
<div class="theme-search-results-item-title">
<div class="card mb-3 p-3">
<h3>
{{ $favorite->tour->title }}
{{ $favorite->favoritable->title }}
</h3>
</div>
<div class="theme-search-results-item-actions">
<a
href="#"
class="btn btn-primary"
>
مشاهده تور
</a>
<form
action="{{ route('users.favorite.destroy', $favorite) }}"
method="POST"
style="display:inline-block;"
>
@csrf
@method('DELETE')
<button
type="submit"
class="btn btn-danger"
>
<i class="fa fa-heart-o"></i>
حذف
</button>
</form>
<img width="200"
src="{{ asset($favorite->favoritable->image) }}">
</div>
</div>
</div>
@elseif($favorite->favoritable_type == 'App\Models\Flight')
<div class="card mb-3 p-3">
<h3>
{{ $favorite->favoritable->airline }}
</h3>
<img width="200"
src="{{ asset($favorite->favoritable->image) }}">
</div>
@endif
@endif
@endforeach
</div>
@else
+15
View File
@@ -1,7 +1,22 @@
<div class="col-md-4">
<div class="card border-0 shadow-sm rounded-4 overflow-hidden h-100">
<form action="{{ route('users.favorite.store') }}" method="POST">
@csrf
<input type="hidden" name="id" value="{{ $tour->id }}">
<input type="hidden"
name="type"
value="App\Models\Tour">
<button class="btn btn-outline-danger">
❤️ افزودن به علاقه‌مندی
</button>
</form>
<div class="flight-image">