make picture attribute translatable

This commit is contained in:
Olivier Legat 2022-01-21 14:26:44 +01:00
parent b524ae0131
commit f023c7df7d
6 changed files with 17 additions and 8 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

@ -38,7 +38,7 @@
<tr>
<td>{{$slide->title}}</td>
<td>{!! $slide->text !!}</td>
<td><img src="{{ asset($slide->picture) }}" height="50px"></td>
<td>@if (!empty($slide->picture))<img src="{{ asset($slide->picture) }}" height="50px">@endif</td>
<td><a href="{{$slide->link}}" target="_slide">{{ $slide->link_label }}</a></td>
<td>{{date_format($slide->date_start, "d/m/Y H:i")}}</td>
<td>{{date_format($slide->date_end, "d/m/Y H:i")}}</td>
@ -66,4 +66,4 @@
@endforeach
</tbody>
</table>
@endsection
@endsection

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;">

View file

@ -30,7 +30,7 @@
<tr>
<td>{{$slide->title}}</td>
<td>{!! $slide->text !!}</td>
<td><img src="{{ asset($slide->picture) }}" height="50px"></td>
<td>@if (!empty($slide->picture))<img src="{{ asset($slide->picture) }}" height="50px">@endif</td>
<td><a href="{{$slide->link}}" target="_slide">{{ $slide->link_label }}</a></td>
<td>{{$slide->date_start}}</td>
<td>{{$slide->date_end}}</td>
@ -65,4 +65,4 @@
@endforeach
</tbody>
</table>
@endsection
@endsection