make picture attribute translatable

This commit is contained in:
Olivier Legat 2022-01-21 14:26:44 +01:00
parent b524ae0131
commit ef393326cd
4 changed files with 13 additions and 4 deletions

View file

@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreSlideRequest;
use App\Lib\Utils;
use App\Models\Slide;
use App\Models\Slider;
@ -86,6 +87,7 @@ class SlidesController extends Controller
public function update(StoreSlideRequest $request, Slide $slide)
{
$slide->fill($request->validated());
$slide->picture = Utils::storeImage($request->picture, $slide->getTable());
if (!$slide->saveOrFail()) {
return redirect()->route('bo.slides.edit', $slide->id)->with('error', trans('La modification a échoué'));

View file

@ -39,6 +39,7 @@ class Slide extends Model
public $translatable = [
'title',
'text',
'picture',
'link',
'link_label',
'alt_text_picture'
@ -47,13 +48,13 @@ class Slide extends Model
protected static function boot()
{
static::creating(function (Slide $slide) {
if ($slide->picture) {
if (is_a($slide->picture,"SplFileInfo")) {
$slide->picture = Utils::storeImage($slide->picture, $slide->getTable());
}
});
static::updating(function (Slide $slide) {
if ($slide->picture) {
if (is_a($slide->picture,"SplFileInfo")) {
$slide->picture = Utils::storeImage($slide->picture, $slide->getTable());
}
});

View file

@ -2,6 +2,7 @@
namespace Database\Factories;
use App\Lib\Utils;
use App\Models\Slide;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
@ -24,6 +25,11 @@ class SlideFactory extends Factory
*/
public function definition()
{
$upload = new SplFileInfo(resource_path('assets/images/image-sample.jpg'));
$slide = new Slide();
$imgpath = Utils::storeImage($upload, $slide->getTable());
return [
'title' => $this->faker->unique()->word(),
'text' => $this->faker->realText(25),
@ -32,7 +38,7 @@ class SlideFactory extends Factory
'link_new_tab' => rand(0, 1),
'date_start' => Carbon::now(),
'date_end' => Carbon::now()->addDays(2),
'picture' => new SplFileInfo(resource_path('assets/images/image-sample.jpg')),
'picture' => $imgpath,
'alt_text_picture' => $this->faker->sentence(10),
];
}

View file

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