fix: update tour management
This commit is contained in:
@@ -7,6 +7,10 @@ use App\Http\Requests\TourStoreRequest;
|
|||||||
use App\Models\Tour;
|
use App\Models\Tour;
|
||||||
use App\Models\TravelType;
|
use App\Models\TravelType;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class TourController extends Controller
|
class TourController extends Controller
|
||||||
{
|
{
|
||||||
@@ -18,23 +22,40 @@ class TourController extends Controller
|
|||||||
|
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
return view('admin.tours.create');
|
$travelTypes = TravelType::all();
|
||||||
|
return view('admin.tours.create', compact('travelTypes'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(TourStoreRequest $request)
|
public function store(TourStoreRequest $request)
|
||||||
{
|
{
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
// Tour::create($request->validated());
|
try {
|
||||||
|
$data = $request->validated();
|
||||||
|
|
||||||
|
// ساخت slug اگر خالی بود
|
||||||
|
if (empty($data['slug'])) {
|
||||||
|
$data['slug'] = Str::slug($data['title']);
|
||||||
|
}
|
||||||
|
|
||||||
$tour = Tour::create($request->only([
|
$tour = Tour::create($data);
|
||||||
'travel_type_id', 'title', 'slug', 'description',
|
|
||||||
'start_date', 'end_date', 'price', 'capacity', 'status'
|
|
||||||
]));
|
|
||||||
|
|
||||||
$this->uploadImages($tour, $request->file('images'));
|
if ($request->hasFile('image')) {
|
||||||
return view('admin.tours.index')
|
$image = $request->file('image');
|
||||||
->with('success', 'تور با موفقیت ایجاد شد.');
|
$newFileName = time() . '.tour-image' . rand(11111, 99999) . '.' . $image->extension();
|
||||||
|
$path = $image->storeAs('tours', $newFileName, 'public');
|
||||||
|
|
||||||
|
$tour->update(['image' => $path]);
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
return redirect()->route('admin.tours.index')->with('success', 'تور با موفقیت ایجاد شد.');
|
||||||
|
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
Log::error($exception);
|
||||||
|
DB::rollBack();
|
||||||
|
return back()->withInput()->withErrors(['general' => 'خطایی در ثبت رخ داد.']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(Tour $tour)
|
public function show(Tour $tour)
|
||||||
@@ -50,25 +71,44 @@ class TourController extends Controller
|
|||||||
|
|
||||||
public function update(TourStoreRequest $request, Tour $tour)
|
public function update(TourStoreRequest $request, Tour $tour)
|
||||||
{
|
{
|
||||||
$tour->update($request->validated());
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$data = $request->validated();
|
||||||
|
|
||||||
// ذخیره فایل و گرفتن مسیر (بدون پسوند)
|
if (empty($data['slug'])) {
|
||||||
$path = $request->file('image')->store('photos', 'public');
|
$data['slug'] = Str::slug($data['title']);
|
||||||
|
}
|
||||||
|
|
||||||
$extension = $request->file('image')->getClientOriginalExtension();
|
if ($request->hasFile('image')) {
|
||||||
|
// حذف عکس قبلی از حافظه
|
||||||
|
if ($tour->image && Storage::disk('public')->exists($tour->image)) {
|
||||||
|
Storage::disk('public')->delete($tour->image);
|
||||||
|
}
|
||||||
|
|
||||||
// ایجاد رکورد
|
$image = $request->file('image');
|
||||||
$tour->files()->create([
|
$newFileName = time() . '.tour-image' . rand(11111, 99999) . '.' . $image->extension();
|
||||||
'name' => $path,
|
$path = $image->storeAs('tours', $newFileName, 'public');
|
||||||
'extension' => $extension,
|
|
||||||
'size' => $request->file('image')->getSize(),
|
|
||||||
'original_name' => $request->file('image')->getClientOriginalName(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
$data['image'] = $path;
|
||||||
|
}
|
||||||
|
|
||||||
return redirect()->route('admin.tours.index')
|
$tour->update($data);
|
||||||
->with('success', 'تور با موفقیت بهروزرسانی شد.');
|
|
||||||
|
DB::commit();
|
||||||
|
return redirect()->route('admin.tours.index')->with('success', 'تور با موفقیت بهروزرسانی شد.');
|
||||||
|
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
Log::error($exception);
|
||||||
|
DB::rollBack();
|
||||||
|
return back()->withInput()->withErrors(['general' => 'خطایی در بهروزرسانی رخ داد.']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeImage(Tour $tour)
|
||||||
|
{
|
||||||
|
// حذف تصویر تکی (اختیاری — میتوانی منطق دلخواه بگذاری)
|
||||||
|
return back()->with('success', 'عملیات حذف تصویر انجام شد.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function destroy(Tour $tour)
|
public function destroy(Tour $tour)
|
||||||
@@ -78,10 +118,4 @@ class TourController extends Controller
|
|||||||
return redirect()->route('admin.tours.index')
|
return redirect()->route('admin.tours.index')
|
||||||
->with('success', 'تور با موفقیت حذف شد.');
|
->with('success', 'تور با موفقیت حذف شد.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeImage(Tour $tour, $image)
|
|
||||||
{
|
|
||||||
// منطق حذف تصویر — بسته به جدول تصاویر
|
|
||||||
return back()->with('success', 'تصویر حذف شد.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user