248 lines
6.0 KiB
PHP
248 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Tours\tourStoreRequest;
|
|
use App\Http\Requests\Admin\Tours\tourUpdateRequest;
|
|
use App\Models\File;
|
|
use App\Models\Tour;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TourController extends Controller
|
|
{
|
|
public function index(\Illuminate\Http\Request $request)
|
|
{
|
|
$tours = Tour::query()
|
|
->with('file')
|
|
|
|
->when($request->filled('search'), function (Builder $query) use ($request) {
|
|
|
|
$search = $request->input('search');
|
|
|
|
$query->whereAny([
|
|
'title',
|
|
'summary',
|
|
'description',
|
|
'origin',
|
|
'destination',
|
|
], 'LIKE', "%$search%");
|
|
|
|
})
|
|
|
|
->when($request->filled('sort'), function (Builder $query) use ($request) {
|
|
|
|
switch ($request->input('sort')) {
|
|
|
|
case 'price_asc':
|
|
$query->orderBy('price');
|
|
break;
|
|
|
|
case 'price_desc':
|
|
$query->orderByDesc('price');
|
|
break;
|
|
|
|
case 'duration_asc':
|
|
$query->orderBy('duration');
|
|
break;
|
|
|
|
case 'duration_desc':
|
|
$query->orderByDesc('duration');
|
|
break;
|
|
|
|
case 'created_at':
|
|
$query->orderByDesc('created_at');
|
|
break;
|
|
}
|
|
|
|
}, function (Builder $query) {
|
|
|
|
$query->orderByDesc('created_at');
|
|
|
|
})
|
|
|
|
->paginate();
|
|
|
|
return view('admin.tours.index', compact('tours'));
|
|
}
|
|
|
|
|
|
public function create()
|
|
{
|
|
return view('admin.tours.create');
|
|
}
|
|
|
|
|
|
public function store(tourStoreRequest $request)
|
|
{
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
|
|
$fileId = null;
|
|
|
|
if ($request->hasFile('image')) {
|
|
|
|
$file = $request->file('image');
|
|
|
|
$path = $file->store('tours', 'public');
|
|
|
|
$uploadedFile = File::create([
|
|
'name' => pathinfo(
|
|
$file->hashName(),
|
|
PATHINFO_FILENAME
|
|
),
|
|
|
|
'extension' => $file->getClientOriginalExtension(),
|
|
|
|
'size' => $file->getSize(),
|
|
|
|
'original_name' => $file->getClientOriginalName(),
|
|
|
|
'path' => $path,
|
|
]);
|
|
|
|
$fileId = $uploadedFile->id;
|
|
}
|
|
|
|
Tour::create([
|
|
'title' => $request->input('title'),
|
|
|
|
'summary' => $request->input('summary'),
|
|
|
|
'description' => $request->input('description'),
|
|
|
|
'origin' => $request->input('origin'),
|
|
|
|
'destination' => $request->input('destination'),
|
|
|
|
'file_id' => $fileId,
|
|
|
|
'duration' => $request->input('duration'),
|
|
|
|
'views' => 0,
|
|
|
|
'price' => $request->input('price'),
|
|
|
|
'status' => $request->input('status'),
|
|
]);
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withErrors([
|
|
'general' => 'خطایی رخ داده است'
|
|
])
|
|
->withInput();
|
|
}
|
|
|
|
return redirect()
|
|
->route('admin.tours.index')
|
|
->withErrors([
|
|
'success' => 'تور با موفقیت ایجاد شد'
|
|
]);
|
|
}
|
|
|
|
|
|
public function edit(Tour $tour)
|
|
{
|
|
$tour->load('file');
|
|
|
|
return view('admin.tours.edit', compact('tour'));
|
|
}
|
|
|
|
|
|
public function update(tourUpdateRequest $request, Tour $tour) {
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
|
|
$fileId = $tour->file_id;
|
|
|
|
if ($request->hasFile('image')) {
|
|
|
|
$file = $request->file('image');
|
|
|
|
$path = $file->store('tours', 'public');
|
|
|
|
$uploadedFile = File::create([
|
|
'name' => pathinfo(
|
|
$file->hashName(),
|
|
PATHINFO_FILENAME
|
|
),
|
|
|
|
'extension' => $file->getClientOriginalExtension(),
|
|
|
|
'size' => $file->getSize(),
|
|
|
|
'original_name' => $file->getClientOriginalName(),
|
|
|
|
'path' => $path,
|
|
]);
|
|
|
|
$fileId = $uploadedFile->id;
|
|
}
|
|
|
|
$tour->update([
|
|
'title' => $request->input('title'),
|
|
|
|
'summary' => $request->input('summary'),
|
|
|
|
'description' => $request->input('description'),
|
|
|
|
'origin' => $request->input('origin'),
|
|
|
|
'destination' => $request->input('destination'),
|
|
|
|
'file_id' => $fileId,
|
|
|
|
'duration' => $request->input('duration'),
|
|
|
|
'price' => $request->input('price'),
|
|
|
|
'status' => $request->input('status'),
|
|
]);
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withErrors([
|
|
'general' => 'خطایی رخ داده است'
|
|
])
|
|
->withInput();
|
|
}
|
|
|
|
return redirect()
|
|
->route('admin.tours.index')
|
|
->withErrors([
|
|
'success' => 'تور با موفقیت ویرایش شد'
|
|
]);
|
|
}
|
|
|
|
|
|
public function destroy(Tour $tour)
|
|
{
|
|
$tour->delete();
|
|
|
|
return redirect()
|
|
->route('admin.tours.index')
|
|
->withErrors([
|
|
'success' => 'تور با موفقیت حذف شد'
|
|
]);
|
|
}
|
|
}
|