feat:complement wishlist for dashboard side
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Account;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Favorite;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class FavoriteController extends Controller
|
||||
{
|
||||
public function toggle($type, $id)
|
||||
{
|
||||
$query = Favorite::where('user_id', Auth::id());
|
||||
|
||||
if ($type === 'flight') {
|
||||
$query->where('flight_id', $id);
|
||||
} elseif ($type === 'tour') {
|
||||
$query->where('tour_id', $id);
|
||||
} else {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$favorite = $query->first();
|
||||
|
||||
if ($favorite) {
|
||||
$favorite->delete();
|
||||
} else {
|
||||
|
||||
$data = [
|
||||
'user_id' => Auth::id(),
|
||||
];
|
||||
|
||||
if ($type === 'flight') {
|
||||
$data['flight_id'] = $id;
|
||||
} else {
|
||||
$data['tour_id'] = $id;
|
||||
}
|
||||
|
||||
Favorite::create($data);
|
||||
}
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Account;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Favorite;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class WishlistController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$favorites = Favorite::query()
|
||||
->where('user_id', Auth::id())
|
||||
->with([
|
||||
'flight.departureAirport.city',
|
||||
'flight.arrivalAirport.city',
|
||||
'flight.file',
|
||||
'tour.file',
|
||||
])
|
||||
->orderByDesc('id')
|
||||
->paginate();
|
||||
|
||||
return view('account.wishlist', compact('favorites'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user