44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
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(string $type, int $id)
|
|
{
|
|
$data = [
|
|
'user_id' => auth()->id(),
|
|
'flight_id' => null,
|
|
'tour_id' => null,
|
|
];
|
|
|
|
if ($type === 'flight') {
|
|
$data['flight_id'] = $id;
|
|
} elseif ($type === 'tour') {
|
|
$data['tour_id'] = $id;
|
|
} else {
|
|
abort(404);
|
|
}
|
|
|
|
$favorite = Favorite::query()
|
|
->where('user_id', auth()->id())
|
|
->where('flight_id', $data['flight_id'])
|
|
->where('tour_id', $data['tour_id'])
|
|
->first();
|
|
|
|
if ($favorite) {
|
|
$favorite->delete();
|
|
|
|
return back()->with('success', 'از علاقهمندیها حذف شد.');
|
|
}
|
|
|
|
Favorite::create($data);
|
|
|
|
return back()->with('success', 'به علاقهمندیها اضافه شد.');
|
|
}
|
|
}
|