Add ranges resource on BO

Add product belongsTo Range
This commit is contained in:
Jérémy Munsch 2021-08-18 17:04:51 +02:00
parent d3283397b6
commit 2e79c7320b
23 changed files with 605 additions and 72 deletions

View file

@ -3,14 +3,16 @@
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Range;
use Illuminate\Http\Request;
class ProduitsController extends Controller
{
public function index() {
$products = Product::all();
if (view()->exists('front.basic.produits-porteurs'))
return view('front.basic.produits-porteurs', compact('products'));
if (view()->exists('front.basic.produits-porteurs')) {
return view('front.basic.produits-porteurs', compact('products', 'ranges'));
}
return redirect()->route('home');
}
}

View file

@ -3,7 +3,10 @@
namespace App\Http\Controllers\bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreProductRequest;
use App\Http\Requests\UpdateProductRequest;
use App\Models\Product;
use App\Models\Range;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
@ -27,32 +30,34 @@ class ProductsController extends Controller
*/
public function create()
{
return view('bo.products.create');
$ranges = Range::all();
if (!count($ranges)) {
return \redirect()->route('bo.ranges.index')
->with(
'error',
'Vous devez ajouter une gamme avant de pouvoir ajouter un produit'
);
}
return view('bo.products.create', compact('ranges'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Http\Requests\StoreProductRequest $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store(StoreProductRequest $request)
{
$name = $request->name;
$slug = Str::slug($name);
$no_art = $request->no_art;
$picture = $request->picture;
$request->validate([
'name' => 'required|string|max:255',
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:' .
'max-width=500' .
',max-height=500',
]);
$product = new Product();
$product->name = $name;
$product->no_art = $no_art;
$product->range()->associate($request->range_id);
$product->save();
if ($picture !== null) {
@ -84,32 +89,34 @@ class ProductsController extends Controller
*/
public function edit($id)
{
$ranges = Range::all();
if (!count($ranges)) {
return \redirect()->route('bo.ranges.index')
->with(
'error',
'Vous devez ajouter une gamme avant de pouvoir ajouter un produit'
);
}
$product = Product::findOrFail($id);
return view('bo.products.edit', compact('product'));
return view('bo.products.edit', compact('product', 'ranges'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Http\Requests\UpdateProductRequest $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(UpdateProductRequest $request, $id)
{
$name = $request->name;
$slug = Str::slug($name);
$no_art = $request->no_art;
$picture = $request->picture;
$request->validate([
'name' => 'required|string|max:255',
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:' .
'max-width=500' .
',max-height=500',
]);
$product = Product::findOrFail($id);
$product->range()->associate($request->range_id);
if ($picture !== null) {
if ($product->picture !== null && file_exists(public_path('images/produits/') . $product->picture))

View file

@ -0,0 +1,106 @@
<?php
namespace App\Http\Controllers\bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreRangeRequest;
use App\Http\Requests\UpdateRangeRequest;
use App\Models\Range;
class RangeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$ranges = Range::paginate();
return view('bo.ranges.index', compact('ranges'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('bo.ranges.create');
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreRangeRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreRangeRequest $request)
{
if (!(new Range($request->validated()))->save()) {
return back()->withErrors('Impossible de sauvegarder la gamme');
}
return \response()->redirectToRoute('bo.ranges.index')
->with('success', 'La gamme à bien été enregistré !');
}
/**
* Display the specified resource.
*
* @param \App\Models\Range $range
* @return \Illuminate\Http\Response
*/
public function show(Range $range)
{
return view('bo.ranges.view', compact('range'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Range $range
* @return \Illuminate\Http\Response
*/
public function edit(Range $range)
{
return view('bo.ranges.update', compact('range'));
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdateRangeRequest $request
* @param \App\Models\Range $range
* @return \Illuminate\Http\Response
*/
public function update(UpdateRangeRequest $request, Range $range)
{
$range->fill($request->validated());
$range->saveOrFail();
return \response()->redirectToRoute('bo.ranges.edit', $range)
->with('success', 'La gamme à bien été mise à jour !');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Range $range
* @return \Illuminate\Http\Response
*/
public function destroy(Range $range)
{
if ($range->products()->count()) {
return back()->withError(
'Cette gamme est associé a des produits, la supression n\'est pas possible'
);
}
if (!$range->delete()) {
return back()->withError(
'Erreur lors de la suppression de la gamme'
);
}
return \response()->redirectToRoute('bo.ranges.index')
->with('warning', 'La gamme à bien été supprimée !');
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:2|max:255',
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=500,max-height=500',
'range_id' => 'exists:ranges,id'
];
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreRangeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:2|max:255'
];
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:2|max:255',
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=500,max-height=500',
'range_id' => 'exists:ranges,id'
];
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRangeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:2|max:255'
];
}
}

View file

@ -6,5 +6,8 @@ use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//
public function range()
{
return $this->belongsTo(Range::class);
}
}

16
app/Models/Range.php Normal file
View file

@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Range extends Model
{
protected $fillable = ['name'];
public function products()
{
return $this->hasMany(Product::class);
}
}

View file

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateModelsRangesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('models_ranges', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('models_ranges');
}
}

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRangesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ranges', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ranges');
}
}

View file

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddRangeIdToProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->foreignId('range_id')->before('created_at')->references('id')
->on('ranges')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropForeign(['range_id']);
$table->dropColumn('range_id');
});
}
}

View file

@ -20,27 +20,7 @@
</div>
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="form-group">
<label for="name">Libelle Produit *</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Libelle Produit" value="{{ old('name') }}" required>
</div>
<div class="form-group">
<label for="no_art">Code Article</label>
<input type="text" id="no_art" name="no_art" class="form-control" placeholder="no_art" value="{{ old('no_art') }}">
</div>
<div class="form-group">
<label for="picture">Image *<small class="text-muted">- JPEG/PNG, 2Mo max. Dimensions {{ Config::get('theme.range_image_maxwidth') }} px par {{ Config::get('theme.range_image_maxheight') }} px</small></label></br>
<input type="file" id="picture" name="picture" accept="image/png, image/jpeg">
</div>
@include('bo.products.input-forms')
</div>
</form>
</div>

View file

@ -23,34 +23,7 @@
</div>
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="form-group">
<label for="name">Libelle Produit *</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Libelle Produit" value="{{ old('name', $product->name) }}" required>
</div>
<div class="form-group">
<label for="no_art">Code Article</label>
<input type="text" id="no_art" name="no_art" class="form-control" placeholder="no_art" value="{{ old('no_art', $product->no_art) }}">
</div>
<div class="form-group">
<label for="picture">Image *<small class="text-muted">- JPEG/PNG, 2Mo max. Dimensions {{ Config::get('theme.range_image_maxwidth') }} px par {{ Config::get('theme.range_image_maxheight') }} px</small></label></br>
<input type="file" id="picture" name="picture" accept="image/png, image/jpeg">
@if ($product->picture !== null)
<br/>
<br/>
<a data-fancybox="gallery" href="{{ asset('/images/produits/' . $product->picture ) }}">
<img src="{{ asset('/images/produits/' . $product->picture ) }}" alt="{{$product->name}}" style="max-height: 100px">
</a>
@endif
</div>
@include('bo.products.input-forms')
</div>
</form>
</div>

View file

@ -43,6 +43,7 @@
<a data-fancybox="gallery" href="{{ asset('/images/produits/' . $product->picture ) }}">
<img src="{{ asset('/images/produits/' . $product->picture ) }}" alt="{{$product->name}}" style="max-height: 50px">
</a>
</td>
<td>
<a href="{{ route('bo.products.edit', $product->id) }}" class="btn btn-sm btn-primary">Modifier</a>
<form action="{{route('bo.products.destroy', $product->id)}}" method="POST" class="d-inline">

View file

@ -0,0 +1,43 @@
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="form-group">
<label for="name">Libelle Produit *</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Libelle Produit" value="{{ old('name', $product->name ?? '') }}" required>
</div>
<div class="form-group">
<label for="no_art">Code Article</label>
<input type="text" id="no_art" name="no_art" class="form-control" placeholder="no_art" value="{{ old('no_art', $product->no_art ?? '') }}">
</div>
<div class="form-group">
<label for="picture">Image *<small class="text-muted">- JPEG/PNG, 2Mo max. Dimensions {{ Config::get('theme.range_image_maxwidth') }} px par {{ Config::get('theme.range_image_maxheight') }} px</small></label></br>
<input type="file" id="picture" name="picture" accept="image/png, image/jpeg">
@if (isset($product->picture) and $product->picture !== null)
<br/>
<br/>
<a data-fancybox="gallery" href="{{ asset('/images/produits/' . $product->picture ) }}">
<img src="{{ asset('/images/produits/' . $product->picture ) }}" alt="{{$product->name}}" style="max-height: 100px">
</a>
@endif
</div>
<div class="form-group">
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="range">{{ __('Gamme') }}</label>
</div>
<select class="custom-select" name="range_id" id="range" required>
@if(!isset($product))
<option selected disabled value="">{{ __('Selectionnez une gamme') }}</option>
@endif
@foreach($ranges as $range)
<option value="{{ $range->id }}" @if(isset($product) and $product->range_id === $range->id) selected @endif>{{ $range->name }}</option>
@endforeach
</select>
</div>
</div>

View file

@ -0,0 +1,27 @@
@extends('layouts.bo')
@section('content')
<div class="card">
<form action="{{route('bo.ranges.store')}}" method="POST" enctype="multipart/form-data">
@csrf
<div class="card-header">
<div class="row">
<div class="col-md-3">
<a href="{{route('bo.ranges.index')}}" class="h2 text-decoration-none"><</a>{{ __('Gammes - Ajout') }}
</div>
<div class="col-md-6 text-center">
</div>
<div class="col-md-3 text-right">
<button type="submit" class="btn btn-primary btn-sm text-white float-right">
{{_('Enregistrer') }}
</button>
</div>
</div>
</div>
<div class="card-body">
@include('bo.ranges.input-forms')
</div>
</form>
</div>
@endsection

View file

@ -0,0 +1,30 @@
@extends('layouts.bo')
@section('content')
<div class="card">
<form action="{{route('bo.ranges.update', $range->id)}}" method="POST" enctype="multipart/form-data">
@method('PUT')
@csrf
<div class="card-header">
<div class="row">
<div class="col-md-4">
<a href="{{route('bo.ranges.index')}}" class="h2 text-decoration-none"><</a>{{ __('Gammes - Modification') }}
</div>
<div class="col-md-5 text-center">
@if (session('success'))
<span class="float-center text-success">{{ session('success') }}</span>
@endif
</div>
<div class="col-md-3 text-right">
<button type="submit" class="btn btn-primary btn-sm text-white float-right">
{{__('Enregistrer') }}
</button>
</div>
</div>
</div>
<div class="card-body">
@include('bo.ranges.input-forms')
</div>
</form>
</div>
@endsection

View file

@ -0,0 +1,53 @@
@extends('layouts.bo')
@section('content')
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-md-3">
{{ __('Gammes') }}
</div>
<div class="col-md-6 text-center">
</div>
<div class="col-md-3 text-right">
<a class="btn btn-primary btn-sm text-white float-right" href="{{route('bo.ranges.create')}}">
{{ __('Ajouter') }}
</a>
</div>
</div>
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<table class="table">
<thead class="thead-light">
<th scope="col">{{ __('Nom') }}</th>
<th scope="col"></th>
</thead>
<tbody>
@foreach($ranges as $range)
<tr>
<td>{{$range->name}}</td>
<td>
<a href="{{ route('bo.ranges.edit', $range->id) }}" class="btn btn-sm btn-primary">{{ __('Modifier') }}</a>
<form action="{{route('bo.ranges.destroy', $range->id)}}" method="POST" class="d-inline">
@method('DELETE')
@csrf
<input type="submit" class="btn btn-sm btn-danger" value="{{ __('Supprimer') }}" onclick="return confirm('{{ __('Êtes-vous sûr(e) ?') }}')">
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endsection

View file

@ -0,0 +1,13 @@
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="form-group">
<label for="name">Nom *</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Nom de la gamme" value="{{ old('name', $range->name ?? '') }}" required>
</div>

View file

@ -87,6 +87,7 @@
<a class="nav-link" href="{{route('bo.instant-win')}}">Instant gagnants</a>
@endsuperadmin
<a class="nav-link" href="{{route('bo.codes-ean')}}">Code EAN</a>
<a class="nav-link" href="{{route('bo.ranges.index')}}">Gammes</a>
<a class="nav-link" href="{{route('bo.products.index')}}">Produits porteurs</a>
@superadmin
<a class="nav-link" href="#_">Social share</a>
@ -113,6 +114,7 @@
</nav>
</div>
<div class="col-md-10">
@include('modules.flashMessage')
@yield('content')
</div>
</div>

View file

@ -0,0 +1,54 @@
@if ($message = Session::get('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>{!! nl2br(e($message)) !!}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
@endif
@if ($message = Session::get('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>{!! nl2br(e($message)) !!}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
@endif
@if ($message = Session::get('warning'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>{!! nl2br(e($message)) !!}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
@endif
@if ($message = Session::get('info'))
<div class="alert alert-info alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong>{!! nl2br(e($message)) !!}</strong>
</div>
@endif
@if ($errors->any())
<div class="alert alert-danger alert-dismissible fade show" role="alert">
@if ($errors->any())
<ul class="list-unstyled">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
@endif

View file

@ -95,6 +95,8 @@ Route::namespace('bo')
Route::post('/winners-list/arcadia/{id}/send', 'WinnersListController@createCourrierArcadia')->name('courrier-arcadia');
//produits porteurs
Route::resource('products',ProductsController::class)->withoutMiddleware('is-super-admin');
Route::resource('ranges', RangeController::class)
->withoutMiddleware('is-super-admin');
// Route::get('/products', 'ProductsController@index')->name('products');
// Route::get('/products/{id}', 'ProductsController@edit')->name('products.edit');