nova-issues
nova-issues copied to clipboard
Images disappear on update Repeater fields
- Laravel Version: v11.8.0
- Nova Version: 4.34.2
- PHP Version: 8.3.7
Description:
When you have an Image field + another one, let's say a Text field) inside a Repeater, the image deletes after performing an update to the other field.
This is how the repeater class looks like:
<?php
namespace App\Nova\Repeater;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Image;
use Laravel\Nova\Fields\Markdown;
use Laravel\Nova\Fields\MultiSelect;
use Laravel\Nova\Fields\Repeater\Repeatable;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
class ImageAndDescription extends Repeatable
{
public $confirmRemoval = true;
public function fields(NovaRequest $request)
{
return [
Boolean::make('Active', 'active'),
Markdown::make('Content', 'content')
->required(),
Text::make('Image title', 'image_title')
->rules('required_with:image_src'),
Image::make('Image', 'image_src')
->maxWidth(100)
->rules('nullable')
// ->disk('s3')
->path("/images/products/{$request->slug}"),
];
}
}
The Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $table = 'products';
protected $fillable = [
'images',
'name',
];
protected $casts = [
'created_at' => 'datetime',
'images' => 'array',
'updated_at' => 'datetime',
];
}
The resource:
<?php
namespace App\Nova;
use App\Nova\Resource;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Repeater;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
class Product extends Resource
{
public static $model = \App\Models\Product::class;
public static $title = 'name';
public static $globallySearchable = false;
public function fields(NovaRequest $request)
{
return [
ID::make()->sortable(),
Text::make('Name', 'name')
->sortable()
->filterable()
->rules('required', 'min:3', 'max:150'),
Repeater::make('Images', 'images')
->repeatables([
\App\Nova\Repeater\ImageAndDescription::make(),
])
->asJson()
->showOnDetail(),
];
}
}
Detailed steps to reproduce the issue on a fresh Nova installation:
- Create a new
Product
in Nova without any repeater field on it, click on [create] - Go to the latest created
Product
and add one repeater item (ImageAndDescription) make sure to upload an image and whatever text too click on [update] - Go back to edit the same
Product
, it would have the image there already (inside the repeater), now edit just the text inside the repeater (don't touch the image at all) and click on [update] to save the resource's data. - Go back to edit the same
Product
, now the text is updated (according to the previous step) but the image is gone (the value in the DB has turned into null)