LaravelRUS / SleepingOwlAdmin

🦉 Administrative interface builder for Laravel (Laravel admin)
http://sleepingowladmin.ru/
MIT License
799 stars 216 forks source link

image inside hasmany #1385

Closed eugenem closed 6 months ago

eugenem commented 10 months ago
    "laravel/framework": "^9.0",
    "laravelrus/sleepingowl": "^9.0",
AdminSection::registerModel(Setting::class, function (ModelConfiguration $model) {
    $model->onEdit(function() {
        $tabs = AdminDisplay::tabbed()->setHtmlAttribute('class', 'card-main-tabs');
             $tabs->appendTab(AdminForm::elements([
                AdminFormElement::text('home_title', 'כותרת'),
               AdminFormElement::hasMany('home_buttons', [
                    AdminFormElement::number('ordr', 'סדר'),
                    AdminFormElement::text('title', 'כותרת'),
                    AdminFormElement::image('image', 'תמונה')
                        ->setUploadSettings([
                            'resize' => [600, null, function ($constraint)
                            {
                                $constraint->upsize();
                                $constraint->aspectRatio();
                            }],
                        ])
                        ->setAfterSaveCallback(function ($value, $model)
                        {
                            $model->image = $value;
                            $model->save();
                        }),

So when I try to use this image in the repeater:

  1. i don't see an image preview after the upload
  2. save fails with the following error, meaning it's trying to save image in Setting, instead of a repeater SQLSTATE[42S22]: Column not found: 1054 Unknown column 'image' in 'field list' update settings set image = uploads/c8cbae4cfbd81a7286b2209f34147f3f.jpg where id = 1
Aglok commented 8 months ago

Switch to the model through registration service provider. The old description does not have access to the current hasMany model and this method has not been supported for a long time. Example:

  $hasMany = AdminForm::form()->addElement(
          new FormElements(
                [
                    AdminFormElement::hasMany('exam_results', [
                        AdminFormElement::image('image', 'Иконка')
                            ->setUploadSettings([
                                'resize' => [600, null, function ($constraint)
                                {
                                    $constraint->upsize();
                                    $constraint->aspectRatio();
                                }],
                            ])
                            ->setAfterSaveCallback(function ($value, $model)
                            {
                                $model->image = $value;
                                $model->save();
                            }),