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() public function index()
{ {
$favorites=Favorite::with('tour') $favorites = Favorite::with('favoritable')
->where('user_id',auth()->id()) ->where('user_id', auth()->id())
->latest() ->latest()
->get(); ->get();
return view('account.users.favorite.index',compact('favorites'));
return view('account.users.favorite.index', compact('favorites'));
} }
public function store(Request $request) public function store(Request $request)
{ {
$request->validate([
'tour_id'=>[
'required',
'exists:tours,id'
],
]);
Favorite::firstOrCreate([ Favorite::firstOrCreate([
'user_id' => auth()->id(), 'user_id' => auth()->id(),
'tour_id' => $request->tour_id, 'favoritable_type' => $request->type,
'favoritable_id' => $request->id,
]); ]);
return back()->with(
'success', return back();
'تور به علاقه‌مندی‌ها اضافه شد.'
);
} }
+8 -3
View File
@@ -7,17 +7,17 @@
namespace App\Models; namespace App\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
* Class Favorite * Class Favorite
* *
* @property int $id * @property int $id
* @property int $user_id * @property int $user_id
* @property string $favoritable_type * @property string $favoritable_type
* @property int $favoritable_id * @property int $favoritable_id
* @property Carbon|null $created_at * @property Carbon|null $created_at
* *
* @property User $user * @property User $user
* *
* @package App\Models * @package App\Models
@@ -43,4 +43,9 @@ class Favorite extends Model
{ {
return $this->belongsTo(User::class); 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); return $this->hasMany(Comment::class);
} }
public function favorites()
{
return $this->morphMany(Favorite::class, 'favoritable');
}
#[Scope] #[Scope]
public function applySearch(Builder $query, array $filters): void public function applySearch(Builder $query, array $filters): void
+4
View File
@@ -62,4 +62,8 @@ class Tour extends Model
{ {
return $this->belongsTo(TravelType::class); return $this->belongsTo(TravelType::class);
} }
public function favorites()
{
return $this->morphMany(Favorite::class, 'favoritable');
}
} }
@@ -93,74 +93,40 @@
@foreach($favorites as $favorite) @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"> <div class="card mb-3 p-3">
@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">
<h3> <h3>
{{ $favorite->tour->title }} {{ $favorite->favoritable->title }}
</h3> </h3>
</div> <img width="200"
src="{{ asset($favorite->favoritable->image) }}">
<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>
</div> </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 @endforeach
</div> </div>
@else @else
+15
View File
@@ -1,7 +1,22 @@
<div class="col-md-4"> <div class="col-md-4">
<div class="card border-0 shadow-sm rounded-4 overflow-hidden h-100"> <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"> <div class="flight-image">