Files

61 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Favorite;
use App\Models\Tour;
use Carbon\Carbon;
use Illuminate\Http\Request;
class TourController extends Controller
{
public function index()
{
$tours = Tour::with('tour_details')->latest()->paginate(6);
$tours->each(function ($tour) {
$detail = $tour->tour_details->first();
$tour->nights = $detail
? Carbon::parse($detail->start_date)->diffInDays(Carbon::parse($detail->end_date))
: null;
});
return view('tours.index', compact('tours'));
}
public function show(Tour $tour)
{
$tour->load([
'tour_details',
'originAirport',
'destinationAirport',
'file',
]);
$detail = $tour->tour_details->first();
$comments = $tour->comments()->latest()->get();
$nights = $detail
? (int) round(Carbon::parse($detail?->start_date)
->diffInDays(Carbon::parse($detail?->end_date)))
: null;
$startTime = verta($detail?->start_date)->format('d-%B');
$endTime = verta($detail?->end_date)->format('d-%B');
$startTravel = verta($detail?->start_date)->format('Y-m-d');
$endTravel = verta($detail?->end_date)->format('Y-m-d');
$startDay = verta($detail?->start_date)->format('%A');
$endDay = verta($detail?->end_date)->format('%A');
$isFavorite = false;
if (auth()->check()) {
$isFavorite = Favorite::where('user_id', auth()->id())
->where('favoritable_id', $tour->id)
->where('favoritable_type', \App\Models\Tour::class)
->exists();
}
return view('tours.show', compact('tour' , 'startTime' , 'endTime' , 'startTravel' ,
'endTravel' , 'startDay' , 'endDay' , 'nights' , 'isFavorite' , 'comments'));
}
}