我认为,正确的做法是让一个Image
模型一个对应的表格,那么你可以设置它与其他模型的关系。例如:
public function store(Request $request)
{
$model = new RelatedModel(); // This is a related model example
$images = $request->file("image.*");
foreach($images as $uploadedImage)
{
$path = $uploadedImage->store('path/images', 'local'); // disk can be null, it will then use the default disk in filesystems.php
$image = new Image();
// A way you want to use to give the image a name
$image->name = $this->generateName();
$image->path = $path;
// Where relatedModel is the method on Image model defining a belongsTo relation for example with RelatedModel
$image->relatedModel()->associate($model);
$image->save();
}
}
我不知道你为什么要按照问题中指定的方式保存图片。但是,如果你坚持,你必须在你的代码添加新字段
id | image1 | image1_name | image2 | image2_name ...
然后:
public function store(Request $request)
{
$model=new Model();
// This is a function you would make to generate a different name than the path
$model->image1_name = $this->generateName();
$model->image1 = $request->file("image.0");->store('path/images', 'local');
$model->image2_name = $this->generateName();
$model->image2 = $request->file("image.1");->store('path/images', 'local');
// ...etc.
$model->save();
}
什么是你需要帮助的具体问题?是的,你可以上传5张图片并将他们的名字保存在数据库中。 –
如果你用不同的id将它们分成不同的记录会更好吗?像ID,文件名,图像(BLOB?),创建时间,更新时间 – Jigs