Php 将多个图像路径存储到Laravel中的数据库中
我已经在我的网站上注册了表格。在许多字段中,有一个字段允许用户上传多个图像。我想将所有图像的路径保存到数据库中,以便在刀片文件中显示它们。 我可以上传图像并将图像存储到本地存储中,但问题是我只能获得一条路径,并将其保存到数据库中 我想将所有路径分别存储到数据库中,以便我可以为Blade访问它们。我该怎么做 刀片Php 将多个图像路径存储到Laravel中的数据库中,php,laravel,laravel-7,Php,Laravel,Laravel 7,我已经在我的网站上注册了表格。在许多字段中,有一个字段允许用户上传多个图像。我想将所有图像的路径保存到数据库中,以便在刀片文件中显示它们。 我可以上传图像并将图像存储到本地存储中,但问题是我只能获得一条路径,并将其保存到数据库中 我想将所有路径分别存储到数据库中,以便我可以为Blade访问它们。我该怎么做 刀片 拉威尔7。PHP 7.4.您需要创建一个数组: public function multiStepStore(Request $request) { $filepath = ar
拉威尔7。PHP 7.4.您需要创建一个数组:
public function multiStepStore(Request $request)
{
$filepath = array(); // $filepath is now an array
if($request->hasFile('photos')) {
$allowedfileExtension = ['jpg', 'png', 'jpeg'];
$files = $request->file('photos');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$path = $filename.'.'.$extension;
Storage::disk('local')->put($filename.'.'.$extension, File::get($file));
$filepath[] = $path; // add new image to array
}
$store_seller = new Sellers();
$store_seller->img_path = $filepath;
dd($filepath); //Returns array with 3 images
}
}
像这样试试-
public function multiStepStore(Request $request)
{
$images[]= Null; // $images array
if($request->hasFile('photos')) {
$allowedfileExtension = ['jpg', 'png', 'jpeg'];
$files = $request->file('photos');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$filepath = $filename.'.'.$extension;
Storage::disk('local')->put($filename.'.'.$extension, File::get($file));
array_push($images, $filepath);
}
$store_seller = new Sellers();
$store_seller->img_path = $filepath;
dd($images); //It will returns array with 3 images.
}
}
它不能正常工作。我又上传了3个文件。它只返回两个同名的路径。其中一个上传的文件是gmail-logo.png。dd($filepath)之后;它返回相同的名称,但有2次。@Shaan现在检查一下,它就可以工作了。错误在于有一个变量名
filepath
,它使这个数组成为一个变量。所以我把它重命名为path
我现在看到所有上传到数组中的图像,这很好。最后一步,若要将它们存储到数据库中,该怎么办?我尝试了内爆函数,但它给了我一个错误。@Shaan如果你只想存储图像名,那么就在foreachOne问题下执行它,获取文件扩展名有什么目的吗。我的意思是,当它将文件名保存到我的存储中,比如“logo.png.png”,看起来很奇怪?是的,我们非常接近。只有两个问题。为什么我得到[0]索引。另外,当我试图内爆阵列时。它返回错误。如何内爆数组并将值作为字符串存储到变量中?@Shaan在内爆函数运行时会收到什么类型的错误消息?@Shaan请尝试以下方法:$string=in爆(',',$images[0]);您建议的代码的唯一问题是它返回0索引,在上载3个图像时,我们看到的是4个值的数组,而不是3。您的代码正在工作的其他信息:)。。查看上面的#stai答案如果我的代码对你有所帮助,请投票支持我的答案。
public function multiStepStore(Request $request)
{
$filepath = array(); // $filepath is now an array
if($request->hasFile('photos')) {
$allowedfileExtension = ['jpg', 'png', 'jpeg'];
$files = $request->file('photos');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$path = $filename.'.'.$extension;
Storage::disk('local')->put($filename.'.'.$extension, File::get($file));
$filepath[] = $path; // add new image to array
}
$store_seller = new Sellers();
$store_seller->img_path = $filepath;
dd($filepath); //Returns array with 3 images
}
}
public function multiStepStore(Request $request)
{
$images[]= Null; // $images array
if($request->hasFile('photos')) {
$allowedfileExtension = ['jpg', 'png', 'jpeg'];
$files = $request->file('photos');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$filepath = $filename.'.'.$extension;
Storage::disk('local')->put($filename.'.'.$extension, File::get($file));
array_push($images, $filepath);
}
$store_seller = new Sellers();
$store_seller->img_path = $filepath;
dd($images); //It will returns array with 3 images.
}
}