z-song / laravel-admin

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

How to store image to the folder name that is new Id after creating the model #5757

Closed hieulvt128 closed 1 year ago

hieulvt128 commented 1 year ago

Description:

Here is my configuration in filesystems.php:

'admin' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

And when I submit a form to create a new Product model with:

$form->image('feature_image', __('Feature image'))->uniqueName();

The image would be stored to storage/app/public/images/{fileName}.jpg and the path is stored from DB being images/{fileName}.jpg. But now I want to store the image to the path that including the new Id of the Product model which has just been created. E.g: The new product is created with id=98 so I stored the image file to folder storage/app/public/images/products/98/{fileName}.jpg and images/products/98/{fileName}.jpg in DB. How I can do this?

Thanks.

styq123 commented 1 year ago

$form->image($column[, $label])->move($dir, $name);

hieulvt128 commented 1 year ago

$form->image($column[, $label])->move($dir, $name);

Thanks for your reply @styq123 But I think your answer does not resolve my problem. How this $dir include the product ID that is created only after the form submission? I try some ways and finally I found out it should like this:

$form->image('feature_image', __('Feature image'))->uniqueName()->move(function($form) {
    return 'images/products/'. $form->model()->id;
});

Thanks

styq123 commented 1 year ago

$form->image($column[, $label])->move($dir, $name);

Thanks for your reply @styq123 But I think your answer does not resolve my problem. How this $dir include the product ID that is created only after the form submission? I try some ways and finally I found out it should like this:

$form->image('feature_image', __('Feature image'))->uniqueName()->move(function($form) {
    return 'images/products/'. $form->model()->id;
});

Thanks

$form->image($column[, $label])->move($dir, $name);

$dir的意思就是你需要上传文件的路径, $name就是你需要定义的文件名字。 这个cms不好用,使用文档太少,且不够灵活。网上有好多中国人写的更加好用的cms后台框架,方便且快捷,只不过用的框架基础是thinkphp或者webman,如果你感兴趣的话,可以搜搜看。