45 lines
937 B
PHP
45 lines
937 B
PHP
<?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();
|
|
}
|
|
}
|