Files

123 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Favorite;
use App\Models\Tour;
use App\Models\Comment;
// اضافه شد
use App\Models\TravelType;
use Illuminate\Http\Request;
class TourController extends Controller
{
public function index(Request $request)
{
$query = Tour::query()
->where('status', 1)
->with(['travelType', 'flights']);
if ($request->filled('travel_type')) {
$query->where('travel_type_id', $request->input('travel_type'));
}
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->input('q') . '%');
}
if ($request->filled('price_min')) {
$query->where('price', '>=', $request->input('price_min'));
}
if ($request->filled('price_max')) {
$query->where('price', '<=', $request->input('price_max'));
}
if ($request->filled('sort')) {
$query->orderBy('price', $request->input('sort') === 'desc' ? 'desc' : 'asc');
} else {
$query->latest();
}
if ($request->routeIs('index')) {
$tours = $query->latest()->limit(4)->get();
return view('index', compact('tours'));
} else {
$tours = $query->latest()->paginate(2);
$travelTypes = TravelType::where('status', 1)->get();
return view('tours.index', compact('tours', 'travelTypes'));
}
}
public function show(Tour $tour)
{
if ($tour->status != 1) {
abort(404);
}
$tour->increment('views_count');
// بارگذاری اطلاعات مورد نیاز + نظرات تایید شده
$tour->load([
'flights',
'travelType',
'comments' => function ($query) {
$query->where('status', 1)->latest();
},
'comments.user' // برای نمایش نام کاربری در نظرات
]);
$relatedTours = Tour::where('status', 1)
->where('travel_type_id', $tour->travel_type_id)
->where('id', '!=', $tour->id)
->latest()
->limit(3)
->get();
$isFav = false;
if (auth()->check()) {
$isFav = Favorite::where('user_id', auth()->id())
->where('favoritable_type', Tour::class)
->where('favoritable_id', $tour->id)
->exists();
}
return view('tours.show', compact('tour', 'relatedTours', 'isFav'));
}
//ثبت نظر جدید برای تور
public function storeComment(Request $request, Tour $tour)
{
$request->validate([
'body' => 'required|string|min:5|max:1000',
]);
$tour->comments()->create([
'user_id' => auth()->id(),
'body' => $request->body,
'status' => 0, // در انتظار تایید
]);
return back()->with('success', 'نظر شما ثبت شد و پس از تایید مدیریت نمایش داده می‌شود.');
}
//جستجوی پیشنهادی
public function searchSuggestion(Request $request)
{
$q = $request->input('q', '');
return response()->json(
Tour::where('status', 1)
->where('title', 'like', '%' . $q . '%')
->limit(8)
->get(['id', 'title', 'slug', 'price'])
);
}
}