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\TravelType;
|
||||
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
|
||||
{
|
||||
@@ -18,23 +22,40 @@ class TourController extends Controller
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('admin.tours.create');
|
||||
$travelTypes = TravelType::all();
|
||||
return view('admin.tours.create', compact('travelTypes'));
|
||||
}
|
||||
|
||||
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([
|
||||
'travel_type_id', 'title', 'slug', 'description',
|
||||
'start_date', 'end_date', 'price', 'capacity', 'status'
|
||||
]));
|
||||
$tour = Tour::create($data);
|
||||
|
||||
$this->uploadImages($tour, $request->file('images'));
|
||||
return view('admin.tours.index')
|
||||
->with('success', 'تور با موفقیت ایجاد شد.');
|
||||
if ($request->hasFile('image')) {
|
||||
$image = $request->file('image');
|
||||
$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)
|
||||
@@ -50,25 +71,44 @@ class TourController extends Controller
|
||||
|
||||
public function update(TourStoreRequest $request, Tour $tour)
|
||||
{
|
||||
$tour->update($request->validated());
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$data = $request->validated();
|
||||
|
||||
// ذخیره فایل و گرفتن مسیر (بدون پسوند)
|
||||
$path = $request->file('image')->store('photos', 'public');
|
||||
if (empty($data['slug'])) {
|
||||
$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);
|
||||
}
|
||||
|
||||
// ایجاد رکورد
|
||||
$tour->files()->create([
|
||||
'name' => $path,
|
||||
'extension' => $extension,
|
||||
'size' => $request->file('image')->getSize(),
|
||||
'original_name' => $request->file('image')->getClientOriginalName(),
|
||||
]);
|
||||
$image = $request->file('image');
|
||||
$newFileName = time() . '.tour-image' . rand(11111, 99999) . '.' . $image->extension();
|
||||
$path = $image->storeAs('tours', $newFileName, 'public');
|
||||
|
||||
$data['image'] = $path;
|
||||
}
|
||||
|
||||
return redirect()->route('admin.tours.index')
|
||||
->with('success', 'تور با موفقیت بهروزرسانی شد.');
|
||||
$tour->update($data);
|
||||
|
||||
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)
|
||||
@@ -78,10 +118,4 @@ class TourController extends Controller
|
||||
return redirect()->route('admin.tours.index')
|
||||
->with('success', 'تور با موفقیت حذف شد.');
|
||||
}
|
||||
|
||||
public function removeImage(Tour $tour, $image)
|
||||
{
|
||||
// منطق حذف تصویر — بسته به جدول تصاویر
|
||||
return back()->with('success', 'تصویر حذف شد.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user