translatable picture in Product model

This commit is contained in:
Olivier Legat 2022-01-23 14:52:07 +01:00
parent 8c76c28291
commit bf66d4d0d3
5 changed files with 25 additions and 21 deletions

View file

@ -63,6 +63,9 @@ class ProductsController extends Controller
{
$plusproduit = json_decode($request->plusproduit);
$product = new Product($request->validated());
if (is_a($request->picture,"SplFileInfo")) {
$product->picture = Utils::storeImage($request->picture, $product->getTable());
}
foreach($plusproduit as $plus) {
$picto = 'pictogramme'.$plus->id;
@ -109,6 +112,9 @@ class ProductsController extends Controller
//valeurs transmises dans le post
$plusproduit = json_decode($request->plusproduit);
$product->fill($request->validated());
if (is_a($request->picture,"SplFileInfo")) {
$product->picture = Utils::storeImage($request->picture, $product->getTable());
}
foreach($plusproduit as $plus) {
//boucle pour stockage des images uploadées

View file

@ -36,9 +36,10 @@ class Product extends Model
'name',
'slug',
'description',
'picture',
'alt_text_picture',
'pictos',
'plusproduit',
'alt_text_picture',
'seo_title',
'seo_description',
];
@ -46,21 +47,15 @@ class Product extends Model
protected static function boot()
{
static::creating(function (Product $product) {
if ($product->picture) {
if (is_a($product->picture,"SplFileInfo")) {
$product->picture = Utils::storeImage($product->picture, $product->getTable());
}
if ($product->banner) {
$product->banner = Utils::storeImage($product->banner, $product->getTable());
}
});
static::updating(function (Product $product) {
if ($product->picture) {
if (is_a($product->picture,"SplFileInfo")) {
$product->picture = Utils::storeImage($product->picture, $product->getTable());
}
if ($product->banner) {
$product->banner = Utils::storeImage($product->banner, $product->getTable());
}
});
static::deleted(function (Product $product) {
@ -72,14 +67,6 @@ class Product extends Model
}
}
}
if ($product->banner) {
$urlPicture = public_path() . $product->banner;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
//delete des images de plus produit
$plusproduit = json_decode($product->plusproduit);
foreach($plusproduit as $plus) {

View file

@ -2,6 +2,8 @@
namespace Database\Factories;
use App\Lib\Utils;
use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use SplFileInfo;
@ -95,11 +97,16 @@ class ProductFactory extends Factory
'name' => 'Lorem ipsum set doloris',
'url' => '/assets/images/sans-label.jpg',
);
$upload = new SplFileInfo(resource_path('assets/images/product_sample_01.png'));
$picto = new Product();
$imgpath = Utils::storeImage($upload, $picto->getTable());
return [
'name' => $name,
'slug' => Str::slug($name),
'description' => $this->faker->paragraph(),
'picture' => new SplFileInfo(resource_path('assets/images/product_sample_01.png')),
'picture' => $imgpath,
'alt_text_picture' => $this->faker->sentence(10),
'pictos' => json_encode([
'four' => $tabFour,

View file

@ -29,7 +29,7 @@
<div class="form-group">
<label for="picture">{{ __('Image') }} <small class="text-muted">- PNG, 1Mo max. Dimensions {{ Config::get('bo.product_image_maxwidth') }} px par {{ Config::get('bo.product_image_maxheight') }} px</small></label><br/>
<input type="file" id="picture" name="picture" accept="image/png">
@if ($product->picture !== null)
@if (!empty($product->picture))
<br/>
<br/>
<img src="{{$product->picture}}" alt="{{$product->alt_text_picture}}" style="max-height: 100px;">

View file

@ -28,7 +28,11 @@
@foreach($products as $product)
<tr>
<td>{{$product->name}}</td>
<td><img src="{{ asset($product->picture) }}" height="50px"></td>
<td>
@if (!empty($product->picture))
<img src="{{ asset($product->picture) }}" height="50px"></td>
@endif
</td>
@if (isset($product->gamme))
<td>
<a href="{{route('bo.gammes.edit', $product->gamme->id) }}">{{$product->gamme->title}}</a>
@ -52,4 +56,4 @@
@endforeach
</tbody>
</table>
@endsection
@endsection