ldc-legaulois/app/Http/Controllers/Bo/SubRangeController.php
Jérémy Munsch a0daf14737 Ticket1001
2022-09-19 07:09:47 +00:00

111 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreSubRange;
use App\Http\Requests\UpdateSubRange;
use App\Models\SubRange;
use Illuminate\Support\Facades\File;
class SubRangeController extends Controller
{
/**
* Viewany subrange
*
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
*/
public function index()
{
$subranges = SubRange::paginate();
return view('bo.subranges.index', compact('subranges'));
}
/**
* Create subrange
*
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
*/
public function create()
{
return view('bo.subranges.create');
}
/**
* Store subrange
*
* @param \App\Http\Requests\StoreSubRange $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreSubRange $request)
{
$subrange = new SubRange($request->validated());
$this->storeUploadedImage($request, $subrange, 'picture');
$subrange->saveOrFail();
return redirect()->route('bo.subranges.index');
}
/**
* Edit subrange
*
* @param \App\Models\SubRange $subrange
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
*/
public function edit(SubRange $subrange)
{
return view('bo.subranges.edit', ['subrange' => $subrange]);
}
/**
* Update subrange
*
* @param \App\Http\Requests\UpdateSubRange $request
* @param \App\Models\SubRange $subrange
* @return \Illuminate\Http\RedirectResponse
*/
public function update(UpdateSubRange $request, SubRange $subrange)
{
$subrange->fill($request->validated());
$this->storeUploadedImage($request, $subrange, 'picture');
$subrange->saveOrFail();
return redirect()->route('bo.subranges.edit', $subrange)
->with('success', 'Enregistré !');
}
/**
* Destroy subrange
*
* @param \App\Models\SubRange $subrange
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(SubRange $subrange)
{
if (File::exists($subrange->picture)) {
File::delete($subrange->picture);
}
if (!$subrange->delete()) {
return redirect()->route('bo.subranges.index')
->with('error', 'Le contenu n\'a pas pu être supprimé');
}
return redirect()->route('bo.subranges.index')
->with('warning', 'Le contenu a bien été supprimé');
}
/**
* Remove subrange picture
*
* @param \App\Models\SubRange $subrange
* @return \Illuminate\Http\RedirectResponse
*/
public function removeImage(SubRange $subrange)
{
if (File::exists($subrange->picture)) {
File::delete($subrange->picture);
}
$subrange->picture = '';
$subrange->saveOrFail();
return redirect()->route('bo.subranges.edit', $subrange)
->with('warning', 'L\'image à bien été supprimée !');
}
}