How to get filename of image (multiple files)

Hi there, please help me to get filename of image (multiple files) here is my code:
Action::make('upload_official')
->icon('heroicon-m-photo')
->form([
FileUpload::make('image')
->multiple()
->previewable(false)
->directory('car-photos-official-attachments')
->image(),
])
->action(function (array $data, Car $car): void {
foreach ($data as $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item['image']
]);
};
}),
Action::make('upload_official')
->icon('heroicon-m-photo')
->form([
FileUpload::make('image')
->multiple()
->previewable(false)
->directory('car-photos-official-attachments')
->image(),
])
->action(function (array $data, Car $car): void {
foreach ($data as $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item['image']
]);
};
}),
Solution:
Okay. this code solved my problem Thanks...
Jump to solution
10 Replies
wandiaprianto
wandiapriantoOP13mo ago
this is my error message
No description
wandiaprianto
wandiapriantoOP13mo ago
this is my car model and photo official model
class Car extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;

/**
* @var string
*/
protected $table = 'cars';

public function official(): HasMany
{
return $this->hasMany(CarOfficial::class, 'car_id');
}

...
}
class Car extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;

/**
* @var string
*/
protected $table = 'cars';

public function official(): HasMany
{
return $this->hasMany(CarOfficial::class, 'car_id');
}

...
}
class CarOfficial extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;

/**
* @var string
*/
protected $table = 'photos_car_official';

public function brand(): BelongsTo
{
return $this->belongsTo(Car::class, 'car_id');
}
}
class CarOfficial extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;

/**
* @var string
*/
protected $table = 'photos_car_official';

public function brand(): BelongsTo
{
return $this->belongsTo(Car::class, 'car_id');
}
}
Vp
Vp13mo ago
What is the result if you use dd($data) I think you need to do like:
foreach ($data as $key => $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item[$key]
]);
};
foreach ($data as $key => $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item[$key]
]);
};
wandiaprianto
wandiapriantoOP13mo ago
still error Sir
No description
Vp
Vp13mo ago
Can you send the result of dd
wandiaprianto
wandiapriantoOP13mo ago
No description
wandiaprianto
wandiapriantoOP13mo ago
Sir, i have done to get file name of image, but i found another error foreach only insert the first of record, where did i wrong ? this is my code:
Action::make('upload_official')
->icon('heroicon-m-photo')
->form([
FileUpload::make('image')
->multiple()
// ->previewable(false)
// ->reorderable()
// ->appendFiles(false)
->directory('car-photos-official-attachments')
->image(),
])
->action(function (array $data, Car $car): void {
$i = 0;
foreach ($data as $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item[$i]
]);

$i++;
};
}),
Action::make('upload_official')
->icon('heroicon-m-photo')
->form([
FileUpload::make('image')
->multiple()
// ->previewable(false)
// ->reorderable()
// ->appendFiles(false)
->directory('car-photos-official-attachments')
->image(),
])
->action(function (array $data, Car $car): void {
$i = 0;
foreach ($data as $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item[$i]
]);

$i++;
};
}),
wandiaprianto
wandiapriantoOP13mo ago
and this is mysql data, only one record inserted, not two
No description
wandiaprianto
wandiapriantoOP13mo ago
should i change my code to this ?
Action::make('upload_official')
->icon('heroicon-m-photo')
->form([
FileUpload::make('image')
->multiple()
// ->previewable(false)
->reorderable()
// ->appendFiles(false)
->directory('car-photos-official-attachments')
->image(),
])
->action(function (array $data, Car $car): void {
$i = 0;
foreach ($data['image'] as $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item
]);

$i++;
};
}),
Action::make('upload_official')
->icon('heroicon-m-photo')
->form([
FileUpload::make('image')
->multiple()
// ->previewable(false)
->reorderable()
// ->appendFiles(false)
->directory('car-photos-official-attachments')
->image(),
])
->action(function (array $data, Car $car): void {
$i = 0;
foreach ($data['image'] as $item) {
CarOfficial::create([
'car_id' => $car->id,
'image' => $item
]);

$i++;
};
}),
Solution
wandiaprianto
wandiaprianto13mo ago
Okay. this code solved my problem Thanks
Want results from more Discord servers?
Add your server