feat: implement favorites for tours and flights
This commit is contained in:
@@ -3,33 +3,101 @@
|
||||
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')
|
||||
->get();
|
||||
return view('account.favorite-list', compact('title','favorites'));
|
||||
}
|
||||
|
||||
public function toggleTour()
|
||||
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 removeTour()
|
||||
public function removeFlight(Flight $flight)
|
||||
{
|
||||
Favorite::where('user_id', auth()->id())
|
||||
->where('flight_id', $flight->id)
|
||||
->delete();
|
||||
|
||||
return back()->with('success', 'پرواز با موفقیت از علاقهمندیها حذف شد.');
|
||||
}
|
||||
|
||||
public function toggleFlight()
|
||||
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);
|
||||
}
|
||||
|
||||
public function removeFlight()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ class ProfileController extends Controller
|
||||
|
||||
public function comments()
|
||||
{
|
||||
$title = 'کامنت های من';
|
||||
$comments = auth()->user()
|
||||
->comments()
|
||||
->with([
|
||||
@@ -36,8 +37,6 @@ class ProfileController extends Controller
|
||||
->orderByDesc('created_at')
|
||||
->paginate(10);
|
||||
|
||||
return view('account.comments' , compact('comments'));
|
||||
return view('account.comments' , compact('title','comments'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+14
-1
@@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class Favorite
|
||||
*
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int|null $tour_id
|
||||
@@ -37,4 +37,17 @@ class Favorite extends Model
|
||||
'tour_id',
|
||||
'flight_id'
|
||||
];
|
||||
|
||||
public function flight()
|
||||
{
|
||||
return $this->belongsTo(Flight::class, 'flight_id');
|
||||
}
|
||||
public function tour()
|
||||
{
|
||||
return $this->belongsTo(Tour::class, 'tour_id');
|
||||
}
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user