firstcommit
This commit is contained in:
0
Modules/Destination/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Destination/app/Http/Controllers/.gitkeep
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Modules\Destination\app\Repositories\DestinationRepository;
|
||||
use Modules\Destination\app\Http\Requests\CreateDestinationRequest;
|
||||
use Modules\Destination\app\Http\Requests\UpdateDestinationRequest;
|
||||
|
||||
class DestinationController extends Controller
|
||||
{
|
||||
protected $destinationRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->destinationRepository = new DestinationRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = $request->has('per-page') ? $request->input('per-page') : null;
|
||||
$filter = $request->has('filter') ? $request->input('filter') : [];
|
||||
$destinations = $this->destinationRepository->allDestinations($perPage, $filter);
|
||||
|
||||
return view('destination::index', compact('destinations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['countries'] = $this->destinationRepository->getCountries() ?? [];
|
||||
$data['activities'] = $this->destinationRepository->getActivities() ?? [];
|
||||
return view('destination::create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(CreateDestinationRequest $request)
|
||||
{
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
$result = $this->destinationRepository->storeDestination($validated);
|
||||
|
||||
if ($result == null) {
|
||||
toastr()->error('Destination not found.');
|
||||
return back();
|
||||
}
|
||||
|
||||
toastr()->success('Destination created successfully.');
|
||||
|
||||
return redirect()->route('cms.destinations.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('destination::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($uuid)
|
||||
{
|
||||
$data['countries'] = $this->destinationRepository->getCountries() ?? [];
|
||||
$data['activities'] = $this->destinationRepository->getActivities() ?? [];
|
||||
$data['destination'] = $this->destinationRepository->destinationWithCountriesAndActivitiesByUuid($uuid);
|
||||
|
||||
if (!$data['destination']) {
|
||||
toastr()->error('Destination not found.');
|
||||
return back();
|
||||
}
|
||||
|
||||
return view('destination::edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(UpdateDestinationRequest $request, $uuid)
|
||||
{
|
||||
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
|
||||
$destination = $this->destinationRepository->updateDestination($validated, $uuid);
|
||||
|
||||
if (!$destination) {
|
||||
toastr()->error('Destination not found!');
|
||||
return back();
|
||||
}
|
||||
|
||||
toastr()->success('Destination updated successfully.');
|
||||
|
||||
return redirect()->route('cms.destinations.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($uuid)
|
||||
{
|
||||
try {
|
||||
$destination = $this->destinationRepository->deleteDestination($uuid);
|
||||
|
||||
if (!$destination) {
|
||||
toastr()->error('Destination not found.');
|
||||
return back();
|
||||
}
|
||||
|
||||
toastr()->success('Destination deleted successfully.');
|
||||
|
||||
return redirect()->route('cms.destinations.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return back();
|
||||
}
|
||||
}
|
||||
}
|
0
Modules/Destination/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Destination/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Destination/app/Http/Requests/.gitkeep
Normal file
0
Modules/Destination/app/Http/Requests/.gitkeep
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
|
||||
class CreateDestinationRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string',
|
||||
'country_id' => 'required|exists:countries,id',
|
||||
'ordering' => 'required|integer|unique:destinations,ordering',
|
||||
'rating' => 'required|integer|between:1,5',
|
||||
'activities' => 'required',
|
||||
'image' => 'sometimes|nullable',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$message = $validator->errors()->first();
|
||||
toastr()->error($message);
|
||||
parent::failedValidation($validator);
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Modules\Destination\app\Models\Destination;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
|
||||
class UpdateDestinationRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$destination = Destination::where('uuid', $this->route('uuid'))->first();
|
||||
|
||||
return [
|
||||
'title' => 'required|string',
|
||||
'country_id' => 'required|exists:countries,id',
|
||||
'ordering' => 'required|integer|unique:destinations,ordering,' . ($destination ? $destination->id : 'NULL') . ',id',
|
||||
'activities' => 'required',
|
||||
'rating' => 'required|integer|between:1,5',
|
||||
'image' => 'sometimes|nullable',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$message = $validator->errors()->first();
|
||||
toastr()->error($message);
|
||||
parent::failedValidation($validator);
|
||||
}
|
||||
}
|
0
Modules/Destination/app/Models/.gitkeep
Normal file
0
Modules/Destination/app/Models/.gitkeep
Normal file
73
Modules/Destination/app/Models/Destination.php
Normal file
73
Modules/Destination/app/Models/Destination.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Modules\Activity\app\Models\Activity;
|
||||
use Modules\CountryList\app\Models\Country;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Modules\Destination\Database\factories\DestinationFactory;
|
||||
|
||||
class Destination extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
// use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'uuid',
|
||||
'country_id',
|
||||
'title',
|
||||
'rating',
|
||||
'ordering',
|
||||
'image',
|
||||
'image_path ',
|
||||
'status ',
|
||||
];
|
||||
|
||||
|
||||
public function country(){
|
||||
return $this->belongsTo(Country::class);
|
||||
}
|
||||
|
||||
public function activities()
|
||||
{
|
||||
return $this->belongsToMany(Activity::class,'activity_destination');
|
||||
}
|
||||
|
||||
// protected static function newFactory(): DestinationFactory
|
||||
// {
|
||||
// //return DestinationFactory::new();
|
||||
// }
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getFullImageAttribute()
|
||||
{
|
||||
$result = null;
|
||||
|
||||
if($this->image_path) {
|
||||
$result = asset('storage/uploads/' . $this->image_path);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getCountryNameAttribute()
|
||||
{
|
||||
$result = null;
|
||||
|
||||
if($this->country_id) {
|
||||
$result = optional($this->country)->name;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
0
Modules/Destination/app/Providers/.gitkeep
Normal file
0
Modules/Destination/app/Providers/.gitkeep
Normal file
114
Modules/Destination/app/Providers/DestinationServiceProvider.php
Normal file
114
Modules/Destination/app/Providers/DestinationServiceProvider.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class DestinationServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Destination';
|
||||
|
||||
protected string $moduleNameLower = 'destination';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register commands in the format of Command::class
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
// $this->commands([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register command Schedules.
|
||||
*/
|
||||
protected function registerCommandSchedules(): void
|
||||
{
|
||||
// $this->app->booted(function () {
|
||||
// $schedule = $this->app->make(Schedule::class);
|
||||
// $schedule->command('inspire')->hourly();
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register translations.
|
||||
*/
|
||||
public function registerTranslations(): void
|
||||
{
|
||||
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
|
||||
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
|
||||
$sourcePath = module_path($this->moduleName, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
|
||||
|
||||
$componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.config('modules.paths.generator.component-class.path'));
|
||||
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function getPublishableViewPaths(): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach (config('view.paths') as $path) {
|
||||
if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->moduleNameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
59
Modules/Destination/app/Providers/RouteServiceProvider.php
Normal file
59
Modules/Destination/app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The module namespace to assume when generating URLs to actions.
|
||||
*/
|
||||
protected string $moduleNamespace = 'Modules\Destination\app\Http\Controllers';
|
||||
|
||||
/**
|
||||
* Called before routes are registered.
|
||||
*
|
||||
* Register any model bindings or pattern based filters.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the routes for the application.
|
||||
*/
|
||||
public function map(): void
|
||||
{
|
||||
$this->mapApiRoutes();
|
||||
|
||||
$this->mapWebRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "web" routes for the application.
|
||||
*
|
||||
* These routes all receive session state, CSRF protection, etc.
|
||||
*/
|
||||
protected function mapWebRoutes(): void
|
||||
{
|
||||
Route::middleware('web')
|
||||
->namespace($this->moduleNamespace)
|
||||
->group(module_path('Destination', '/routes/web.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapApiRoutes(): void
|
||||
{
|
||||
Route::prefix('api')
|
||||
->middleware('api')
|
||||
->namespace($this->moduleNamespace)
|
||||
->group(module_path('Destination', '/routes/api.php'));
|
||||
}
|
||||
}
|
191
Modules/Destination/app/Repositories/DestinationRepository.php
Normal file
191
Modules/Destination/app/Repositories/DestinationRepository.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Repositories;
|
||||
|
||||
use Throwable;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Activity\app\Models\Activity;
|
||||
use Modules\CountryList\app\Models\Country;
|
||||
use Modules\Destination\app\Models\Destination;
|
||||
use Modules\Banner\app\Services\FileManagementService;
|
||||
|
||||
class DestinationRepository
|
||||
{
|
||||
//-- Get All Destination
|
||||
public function allDestinations($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
||||
{
|
||||
return Destination::with('country')
|
||||
->withCount('activities')
|
||||
->when(array_keys($filter, true), function ($query) use ($filter) {
|
||||
if (!empty($filter['title'])) {
|
||||
$query->where('title', $filter['title']);
|
||||
}
|
||||
if (!empty($filter['caption'])) {
|
||||
$query->where('caption', 'like', '%' . $filter['caption'] . '%');
|
||||
}
|
||||
})
|
||||
->orderBy($sort['by'], $sort['sort'])
|
||||
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
||||
}
|
||||
|
||||
public function findDestinationById($uuid)
|
||||
{
|
||||
return Destination::where('uuid', $uuid)->first();
|
||||
}
|
||||
|
||||
//-- Store Destination
|
||||
public function storeDestination($validated)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$destination = new Destination();
|
||||
$destination->uuid = Str::uuid();
|
||||
$destination->title = $validated['title'];
|
||||
$destination->country_id = $validated['country_id'];
|
||||
$destination->rating = $validated['rating'];
|
||||
$destination->ordering = $validated['ordering'];
|
||||
$destination->save();
|
||||
|
||||
//-- get activities
|
||||
if (isset($validated['activities'][0])) {
|
||||
$activityNames = json_decode($validated['activities'][0], true);
|
||||
$activityIds = Activity::whereIn('name', $activityNames)
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
|
||||
$destination->activities()->attach($activityIds);
|
||||
}
|
||||
|
||||
//-- store image
|
||||
if (isset($validated['image']) && $validated['image']->isValid()) {
|
||||
FileManagementService::storeFile(
|
||||
file: $validated['image'],
|
||||
uploadedFolderName: 'banners',
|
||||
model: $destination
|
||||
);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $destination;
|
||||
} catch (Throwable $th) {
|
||||
DB::rollback();
|
||||
report($th);
|
||||
}
|
||||
}
|
||||
|
||||
//-- Update Destination
|
||||
public function updateDestination($validated, $uuid)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$destination = $this->findDestinationById($uuid);
|
||||
if (!$destination) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$destination->title = $validated['title'];
|
||||
$destination->country_id = $validated['country_id'];
|
||||
$destination->rating = $validated['rating'];
|
||||
$destination->ordering = $validated['ordering'];
|
||||
$destination->save();
|
||||
|
||||
//-- update image
|
||||
if (isset($validated['image']) && $validated['image']->isValid()) {
|
||||
FileManagementService::uploadFile(
|
||||
file: $validated['image'],
|
||||
uploadedFolderName: 'destinations',
|
||||
filePath: $destination->image_path,
|
||||
model: $destination
|
||||
);
|
||||
}
|
||||
|
||||
//-- get activities
|
||||
if (isset($validated['activities'][0])) {
|
||||
$activityNames = json_decode($validated['activities'][0], true);
|
||||
$activityIds = Activity::whereIn('name', $activityNames)
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
|
||||
$destination->activities()->sync($activityIds);
|
||||
}
|
||||
return $destination;
|
||||
} catch (Throwable $th) {
|
||||
DB::rollback();
|
||||
report($th);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//-- Delete Destination
|
||||
public function deleteDestination($uuid)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$destination = $this->findDestinationById($uuid);
|
||||
|
||||
if (!$destination) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Delete the image file associated with the destination
|
||||
if ($destination->image_path !== null) {
|
||||
FileManagementService::deleteFile($destination->image_path);
|
||||
}
|
||||
|
||||
$destination->delete();
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollback();
|
||||
report($th);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//-- get countries
|
||||
public function getCountries()
|
||||
{
|
||||
return Country::all();
|
||||
}
|
||||
|
||||
//-- get activities
|
||||
public function getActivities()
|
||||
{
|
||||
return Activity::all();
|
||||
}
|
||||
|
||||
//-- get destination With Countries And Activities By Uuid
|
||||
public function destinationWithCountriesAndActivitiesByUuid($uuid)
|
||||
{
|
||||
return Destination::with('country', 'activities')
|
||||
->where('uuid', $uuid)
|
||||
->first();
|
||||
}
|
||||
|
||||
//-- Delete destination
|
||||
public function destinationRepository(string $uuid)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$destination = $this->findDestinationById($uuid);
|
||||
if (! $destination) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Delete the image file associated with the activity
|
||||
if ($destination->image_path !== null) {
|
||||
FileManagementService::deleteFile($destination->image_path);
|
||||
}
|
||||
$destination->delete();
|
||||
|
||||
return $destination;
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollBack();
|
||||
report($th);
|
||||
}
|
||||
}
|
||||
}
|
66
Modules/Destination/app/Services/FileManagementService.php
Normal file
66
Modules/Destination/app/Services/FileManagementService.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Services;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class FileManagementService
|
||||
{
|
||||
//-- store file
|
||||
public static function storeFile($file, $uploadedFolderName, $model)
|
||||
{
|
||||
try {
|
||||
$originalFileName = $file->getClientOriginalName();
|
||||
$modifiedFileName = date('YmdHis') . "_" . uniqid() . "." . $originalFileName;
|
||||
|
||||
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
|
||||
|
||||
$model->image = $modifiedFileName;
|
||||
$model->image_path = $uploadedFolderName . '/' . $modifiedFileName;
|
||||
$model->save();
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
//-- update file
|
||||
public static function uploadFile($file, $uploadedFolderName ,$filePath, $model)
|
||||
{
|
||||
try {
|
||||
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
|
||||
Storage::disk('public_uploads')->delete($filePath);
|
||||
}
|
||||
|
||||
$originalFileName = $file->getClientOriginalName();
|
||||
$modifiedFileName = date('YmdHis') . "_" . uniqid() . "." . $originalFileName;
|
||||
|
||||
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
|
||||
|
||||
$model->image = $modifiedFileName;
|
||||
$model->image_path = $uploadedFolderName . '/' . $modifiedFileName;
|
||||
|
||||
$model->save();
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-- delete file
|
||||
public static function deleteFile($filePath)
|
||||
{
|
||||
try {
|
||||
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
|
||||
Storage::disk('public_uploads')->delete($filePath);
|
||||
} else {
|
||||
toastr()->error('File Not wrong.');
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong while deleting the file.');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user