z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

How to add type field while uploading MultipleImages using relationship #5807

Open asadkamran opened 1 year ago

asadkamran commented 1 year ago

Description:

ITS NOT ISSUE, JUST A PROBLEM I AM FACING.

How i can set the 'type' field value in vehicle_images table to 'exterior_image', so i can identify its exterior or interior images, i am using multipleImage, and wants to upload exterior and interior image separately.

in Vehicles Model:

public function vehicleImages()
  {
      return $this->hasMany(VehicleImage::class);
  }

In Vehicle Controller:

$form->multipleImage('vehicleImages',__('Vehicle Images'))->pathColumn('file')->sortable()->move('vehicles')->uniqueName()->removable();

my goal is to set type ('interior_image' or 'exterior_image') for each image uploaded.

Yanghsuanming commented 1 year ago
#table schema
create table vehicle_images (
   id int auto_increment primary key,
   vehicle_id int,
   img varchar(255),
   img_type varchar(20),
   created_at datetime,
   updated_at datetime
)

# in form
        $form->hasMany('vehicleImages', __('content'), function($form){
            $form->text('img_type', __('type'))->attribute('maxlength', 32)->rules('required');
            $form->image('img', __('img'))->name(function($file){
                $rand = rand(0, 1000);
                return "{$rand}-".$file->getClientOriginalName();
            });
        });

that's it.

technilogics commented 1 year ago

Thank you for providing a solution. i see it may help in case we add note/description with image.

technilogics commented 1 year ago

It may help some one, to achieve i created 2 models, Model 1: `namespace App\Admin\Models;

use Illuminate\Database\Eloquent\Model;

class VehicleInteriorImage extends Model { protected $table="vehicle_images"; /**

} `

Model 2: ` namespace App\Admin\Models;

use Illuminate\Database\Eloquent\Model;

class VehicleExteriorImage extends Model { protected $table="vehicle_images"; /**

} `

` $form->multipleImage('vehicleExteriorImages',('Exterior Image'))->pathColumn('file')->sortable()->move('vehicles')->uniqueName()->removable(); $form->multipleImage('vehicleInteriorImages',('Interior Image'))->pathColumn('file')->sortable()->move('vehicles')->uniqueName()->removable();

`