82 lines
2.1 KiB
PHP
82 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\CommentStatus;
|
|
use App\Http\Requests\Tour\TourCommentStoreRequest;
|
|
use App\Models\Comment;
|
|
use App\Models\Favorite;
|
|
use App\Models\Tour;
|
|
use Illuminate\Http\Request;
|
|
|
|
class TourController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$title = 'تورها';
|
|
|
|
$sort = $request->query('sort', 'default');
|
|
|
|
$tours = Tour::with([
|
|
'originCity',
|
|
'city',
|
|
'tourDepartures',
|
|
])
|
|
->applySort($sort)
|
|
->paginate(3)
|
|
->withQueryString();
|
|
|
|
return view('tours.index', compact('title', 'tours', 'sort'));
|
|
}
|
|
|
|
public function show(Tour $tour)
|
|
{
|
|
$title = 'جزئیات تور';
|
|
$tour->load([
|
|
'file',
|
|
'originCity',
|
|
'city',
|
|
'tourDepartures',
|
|
'comments' => function ($query) {
|
|
$query->latest();
|
|
},
|
|
'comments.user'
|
|
]);
|
|
$tour->increment('views_count');
|
|
|
|
$isFavorite = false;
|
|
|
|
if (auth()->check()) {
|
|
$isFavorite = Favorite::where('user_id', auth()->id())
|
|
->where('tour_id', $tour->id)
|
|
->exists();
|
|
}
|
|
|
|
return view('tours.show', compact('title', 'tour', 'isFavorite'));
|
|
}
|
|
|
|
public function commentStore(TourCommentStoreRequest $request, Tour $tour)
|
|
{
|
|
|
|
try {
|
|
Comment::create([
|
|
'tour_id' => $tour->id,
|
|
'user_id' => auth()->id(),
|
|
'parent_id' => $request->parent_id,
|
|
'guest_name' => auth()->check() ? null : $request->guest_name,
|
|
'guest_email' => auth()->check() ? null : $request->guest_email,
|
|
'content' => $request->content,
|
|
'status' => CommentStatus::PENDING,
|
|
]);
|
|
|
|
return back()->with('success', 'نظر شما با موفقیت ثبت شد.');
|
|
} catch (\Exception $e) {
|
|
return back()
|
|
->withInput()
|
|
->with('error', 'ثبت نظر با خطا مواجه شد.');
|
|
|
|
}
|
|
}
|
|
|
|
}
|