fix: update tour functionality
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\Favorite;
|
use App\Models\Favorite;
|
||||||
use App\Models\Tour;
|
use App\Models\Tour;
|
||||||
|
use App\Models\Comment;
|
||||||
|
|
||||||
|
// اضافه شد
|
||||||
use App\Models\TravelType;
|
use App\Models\TravelType;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
@@ -18,95 +22,55 @@ class TourController extends Controller
|
|||||||
->where('status', 1)
|
->where('status', 1)
|
||||||
->with(['travelType', 'flights']);
|
->with(['travelType', 'flights']);
|
||||||
|
|
||||||
// فیلتر بر اساس نوع سفر
|
|
||||||
if ($request->filled('travel_type')) {
|
if ($request->filled('travel_type')) {
|
||||||
$query->where(
|
$query->where('travel_type_id', $request->input('travel_type'));
|
||||||
'travel_type_id',
|
|
||||||
$request->input('travel_type')
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// جستجو بر اساس عنوان
|
|
||||||
if ($request->filled('q')) {
|
if ($request->filled('q')) {
|
||||||
$query->where(
|
$query->where('title', 'like', '%' . $request->input('q') . '%');
|
||||||
'title',
|
|
||||||
'like',
|
|
||||||
'%' . $request->input('q') . '%'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// حداقل قیمت
|
|
||||||
if ($request->filled('price_min')) {
|
if ($request->filled('price_min')) {
|
||||||
$query->where(
|
$query->where('price', '>=', $request->input('price_min'));
|
||||||
'price',
|
|
||||||
'>=',
|
|
||||||
$request->input('price_min')
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// حداکثر قیمت
|
|
||||||
if ($request->filled('price_max')) {
|
if ($request->filled('price_max')) {
|
||||||
$query->where(
|
$query->where('price', '<=', $request->input('price_max'));
|
||||||
'price',
|
|
||||||
'<=',
|
|
||||||
$request->input('price_max')
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// مرتبسازی
|
|
||||||
if ($request->filled('sort')) {
|
if ($request->filled('sort')) {
|
||||||
$query->orderBy(
|
$query->orderBy('price', $request->input('sort') === 'desc' ? 'desc' : 'asc');
|
||||||
'price',
|
|
||||||
$request->input('sort') === 'desc'
|
|
||||||
? 'desc'
|
|
||||||
: 'asc'
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
$query->latest();
|
$query->latest();
|
||||||
}
|
}
|
||||||
|
|
||||||
// صفحهبندی
|
$tours = $query->paginate(9);
|
||||||
$tours = $query->paginate(12)->withQueryString();
|
|
||||||
|
|
||||||
// انواع سفر
|
|
||||||
$travelTypes = TravelType::where('status', 1)->get();
|
$travelTypes = TravelType::where('status', 1)->get();
|
||||||
|
|
||||||
return view(
|
return view('tours.index', compact('tours', 'travelTypes'));
|
||||||
'tours.index',
|
|
||||||
compact('tours', 'travelTypes')
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* حذف فیلترها
|
|
||||||
*/
|
|
||||||
public function removeFilters()
|
|
||||||
{
|
|
||||||
return redirect()->route('tours.index');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* نمایش جزئیات یک تور
|
* نمایش جزئیات یک تور
|
||||||
*/
|
*/
|
||||||
public function show(Tour $tour)
|
public function show(Tour $tour)
|
||||||
{
|
{
|
||||||
// فقط تورهای فعال قابل مشاهده باشند
|
|
||||||
if ($tour->status != 1) {
|
if ($tour->status != 1) {
|
||||||
abort(404);
|
abort(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// افزایش تعداد بازدید
|
|
||||||
$tour->increment('views_count');
|
$tour->increment('views_count');
|
||||||
|
|
||||||
// بارگذاری اطلاعات مورد نیاز
|
// بارگذاری اطلاعات مورد نیاز + نظرات تایید شده
|
||||||
$tour->load([
|
$tour->load([
|
||||||
'flights',
|
'flights',
|
||||||
'travelType',
|
'travelType',
|
||||||
|
'comments' => function ($query) {
|
||||||
|
$query->where('status', 1)->latest();
|
||||||
|
},
|
||||||
|
'comments.user' // برای نمایش نام کاربری در نظرات
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// تورهای مرتبط
|
|
||||||
$relatedTours = Tour::where('status', 1)
|
$relatedTours = Tour::where('status', 1)
|
||||||
->where('travel_type_id', $tour->travel_type_id)
|
->where('travel_type_id', $tour->travel_type_id)
|
||||||
->where('id', '!=', $tour->id)
|
->where('id', '!=', $tour->id)
|
||||||
@@ -115,7 +79,6 @@ class TourController extends Controller
|
|||||||
->get();
|
->get();
|
||||||
|
|
||||||
$isFav = false;
|
$isFav = false;
|
||||||
|
|
||||||
if (auth()->check()) {
|
if (auth()->check()) {
|
||||||
$isFav = Favorite::where('user_id', auth()->id())
|
$isFav = Favorite::where('user_id', auth()->id())
|
||||||
->where('favoritable_type', Tour::class)
|
->where('favoritable_type', Tour::class)
|
||||||
@@ -123,35 +86,191 @@ class TourController extends Controller
|
|||||||
->exists();
|
->exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return view('tours.show', compact('tour', 'relatedTours', 'isFav'));
|
||||||
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)
|
public function searchSuggestion(Request $request)
|
||||||
{
|
{
|
||||||
$q = $request->input('q', '');
|
$q = $request->input('q', '');
|
||||||
|
return response()->json(
|
||||||
$tours = Tour::where('status', 1)
|
Tour::where('status', 1)
|
||||||
->where(
|
->where('title', 'like', '%' . $q . '%')
|
||||||
'title',
|
->limit(8)
|
||||||
'like',
|
->get(['id', 'title', 'slug', 'price'])
|
||||||
'%' . $q . '%'
|
);
|
||||||
)
|
|
||||||
->limit(8)
|
|
||||||
->get([
|
|
||||||
'id',
|
|
||||||
'title',
|
|
||||||
'slug',
|
|
||||||
'price'
|
|
||||||
]);
|
|
||||||
|
|
||||||
return response()->json($tours);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
//namespace App\Http\Controllers;
|
||||||
|
//
|
||||||
|
//use App\Models\Favorite;
|
||||||
|
//use App\Models\Tour;
|
||||||
|
//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();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // صفحهبندی
|
||||||
|
// $tours = $query->paginate(9);
|
||||||
|
//
|
||||||
|
// // انواع سفر
|
||||||
|
// $travelTypes = TravelType::where('status', 1)->get();
|
||||||
|
//
|
||||||
|
// return view(
|
||||||
|
// 'tours.index',
|
||||||
|
// compact('tours', 'travelTypes')
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * حذف فیلترها
|
||||||
|
// */
|
||||||
|
// public function removeFilters()
|
||||||
|
// {
|
||||||
|
// return redirect()->route('tours.index');
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * نمایش جزئیات یک تور
|
||||||
|
// */
|
||||||
|
// public function show(Tour $tour)
|
||||||
|
// {
|
||||||
|
// // فقط تورهای فعال قابل مشاهده باشند
|
||||||
|
// if ($tour->status != 1) {
|
||||||
|
// abort(404);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // افزایش تعداد بازدید
|
||||||
|
// $tour->increment('views_count');
|
||||||
|
//
|
||||||
|
// // بارگذاری اطلاعات مورد نیاز
|
||||||
|
// $tour->load([
|
||||||
|
// 'flights',
|
||||||
|
// 'travelType',
|
||||||
|
// ]);
|
||||||
|
//
|
||||||
|
// // تورهای مرتبط
|
||||||
|
// $relatedTours = Tour::where('status', 1)
|
||||||
|
// ->where('travel_type_id', $tour->travel_type_id)
|
||||||
|
// ->where('id', '!=', $tour->id)
|
||||||
|
// ->latest()
|
||||||
|
// ->limit(4)
|
||||||
|
// ->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 searchSuggestion(Request $request)
|
||||||
|
// {
|
||||||
|
// $q = $request->input('q', '');
|
||||||
|
//
|
||||||
|
// $tours = Tour::where('status', 1)
|
||||||
|
// ->where(
|
||||||
|
// 'title',
|
||||||
|
// 'like',
|
||||||
|
// '%' . $q . '%'
|
||||||
|
// )
|
||||||
|
// ->limit(8)
|
||||||
|
// ->get([
|
||||||
|
// 'id',
|
||||||
|
// 'title',
|
||||||
|
// 'slug',
|
||||||
|
// 'price'
|
||||||
|
// ]);
|
||||||
|
//
|
||||||
|
// return response()->json($tours);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|||||||
@@ -75,6 +75,16 @@ class Tour extends Model
|
|||||||
->withPivot('id')
|
->withPivot('id')
|
||||||
->withTimestamps();
|
->withTimestamps();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// در فایل App\Models\Tour.php و App\Models\Flight.php
|
||||||
|
|
||||||
|
public function comments()
|
||||||
|
{
|
||||||
|
return $this->morphMany(Comment::class, 'commentable');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected static function booted()
|
protected static function booted()
|
||||||
{
|
{
|
||||||
static::creating(function ($tour) {
|
static::creating(function ($tour) {
|
||||||
|
|||||||
Reference in New Issue
Block a user