104 lines
2.9 KiB
PHP
104 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Account;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Favorite;
|
|
use App\Models\Flight;
|
|
use App\Models\Tour;
|
|
use Illuminate\Http\Request;
|
|
|
|
class FavoriteController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$title = 'علاقه مندی های من';
|
|
$favorites = Favorite::query()
|
|
->where('user_id', auth()->id())
|
|
->with([
|
|
'tour.file',
|
|
'tour.originCity',
|
|
'tour.city',
|
|
'tour.tourDepartures',
|
|
'flight.airline',
|
|
'flight.origin_airport.city',
|
|
'flight.airport.city',
|
|
])
|
|
->orderByDesc('created_at')
|
|
->paginate(3);
|
|
return view('account.favorite-list', compact('title','favorites'));
|
|
}
|
|
|
|
public function removeTour($tourId)
|
|
{
|
|
Favorite::where('user_id', auth()->id())
|
|
->where('tour_id', $tourId)
|
|
->delete();
|
|
return back()->with('success', 'با موفقیت حذف شد.');
|
|
}
|
|
public function toggleTour(Tour $tour)
|
|
{
|
|
|
|
if (!auth()->check()) {
|
|
return back()->with('error', 'برای افزودن به علاقهمندی ها ابتدا وارد شوید.');
|
|
}
|
|
|
|
$userId = auth()->id();
|
|
|
|
|
|
$favorite = Favorite::where('user_id', $userId)
|
|
->where('tour_id', $tour->id)
|
|
->first();
|
|
|
|
if ($favorite) {
|
|
$favorite->delete();
|
|
$message = 'از علاقهمندیها حذف شد.';
|
|
} else {
|
|
Favorite::create([
|
|
'user_id' => $userId,
|
|
'tour_id' => $tour->id,
|
|
]);
|
|
$message = 'به علاقهمندیها اضافه شد.';
|
|
}
|
|
|
|
return back()->with('success', $message);
|
|
}
|
|
|
|
public function removeFlight(Flight $flight)
|
|
{
|
|
Favorite::where('user_id', auth()->id())
|
|
->where('flight_id', $flight->id)
|
|
->delete();
|
|
|
|
return back()->with('success', 'پرواز با موفقیت از علاقهمندیها حذف شد.');
|
|
}
|
|
|
|
public function toggleFlight(Flight $flight)
|
|
{
|
|
if (!auth()->check()) {
|
|
return back()->with('error', 'برای افزودن پرواز به علاقهمندیها ابتدا وارد شوید.');
|
|
}
|
|
|
|
$userId = auth()->id();
|
|
|
|
$favorite = Favorite::where('user_id', $userId)
|
|
->where('flight_id', $flight->id)
|
|
->first();
|
|
|
|
if ($favorite) {
|
|
$favorite->delete();
|
|
$message = 'پرواز از علاقهمندیها حذف شد.';
|
|
} else {
|
|
Favorite::create([
|
|
'user_id' => $userId,
|
|
'flight_id' => $flight->id,
|
|
]);
|
|
$message = 'پرواز به لیست علاقهمندیها اضافه شد.';
|
|
}
|
|
|
|
return back()->with('success', $message);
|
|
}
|
|
|
|
|
|
}
|