首页 文章

Laravel:使用干预在foreach循环中上传包含图像的表单数据

提问于
浏览
5

我正在使用Laravel 5.1,并希望有一个带有几行文本字段的表单及其相应的图像上传 .

形成:

<div id="row">
    <input name="description[]" type="text">
    <input name="image[]" type="file">
</div>
<div id="row">
    <input name="description[]" type="text">
    <input name="image[]" type="file">
</div>

控制器:

foreach($request->description as $key => $val){

if($val != null){
    $data = new Finding;
    $data->description = $val; //save description for each loop

    $file = array('image' => Input::file('image'));

    if (Input::file('image')->isValid()) {

        $destinationPath = 'uploads';
        $extension = Input::file('image')->getClientOriginalExtension();
        $fileName = rand(1000,1000).'.'.$extension;
        Input::file('image')->move($destinationPath, $fileName);
        $path = Input::file('image')->getRealPath();

        $data->image_location = $fileName[$key]; //save filename location to db
        $data->save();

        flash('success', 'Uploaded Successful');
        return Redirect::to('/upload');
     } else {
        flash('error', 'Uploaded File Is Not Valid');
        return Redirect::to('/upload');
    }
}

我的问题是,如何使用 $key 值的干预来保存表中的新行以及相关的文本描述和图像上载,并仍然使用所有干预类?我的代码关闭了吗?

我可以通过一个表单输入和一个图像上传轻松完成所有这些操作,但我的目标是让一个页面包含多行输入 . 谢谢!

1 回答

  • 5

    我不确定你是如何管理多个领域的,无论它是一成不变的还是可以动态改变的 . 我之前使用javascript完成了动态方法,并跟踪隐藏文本字段作为计数所需的动态字段数 .

    使用这种方法你的html可能看起来像这样:

    <input name="imageAndDescriptionCount" type="hidden" value="2">
    <div id="row">
        <input name="description[]" type="text">
        <input name="image[]" type="file">
    </div>
    <div id="row">
        <input name="description[]" type="text">
        <input name="image[]" type="file">
    </div>
    

    所以这里我们每个都有两个图像和描述字段,计数器设置为2 .

    如果我们提交表单并通过 dd() 签出请求,它看起来像这样:

    "imageAndDescriptionCount" => "2"
    "description" => array:2 [▼
        0 => "description"
        1 => "description"
    ]
        "image" => array:2 [▼
        0 => UploadedFile {#154 ▶}
        1 => UploadedFile {#155 ▶}
    ]
    

    然后,这可以很好地为您的控制器中的for循环设置:

    $count = $request->get('imageAndDescriptionCount');
    $description = $request->get('description'); // assign array of descriptions
    $image = $request->file('image'); // assign array of images
    
    // set upload path using https://laravel.com/docs/5.1/helpers#method-storage-path 
    // make sure 'storage/uploads' exists first
    $destinationPath = storage_path . '/uploads'; 
    
    for($i = 0; $i < $count; $i++) {
    
        $data = new Finding;
        $data->description = [$i];
    
        $file = $image[$i];
    
        if ($file->isValid()) {
            $extension = $file->getClientOriginalExtension(); // file extension
            $fileName = uniqid(). '.' .$extension; // file name with extension
            $file->move($destinationPath, $fileName); // move file to our uploads path
    
            $data->image_location = $fileName;
            // or you could say $destinationPath . '/' . $fileName
            $data->save();
        } else {
            // handle error here
        }
    
    }
    
    flash('success', 'Uploads Successful');
    return Redirect::to('/upload');
    

    请注意,据我所知,您的代码中没有使用干预库,我只是编写了代码来执行上传 . 我希望这可以帮助你!

    Edit

    如果你真的想使用foreach循环并且你知道你将对每个图像都有描述,你可以这样做(但我个人认为有一个计数器是一个更好的方法)

    $descriptions = $request->get('description'); // assign array of descriptions
    $images = $request->file('image'); // assign array of images
    
    // loop through the descriptions array
    foreach($descriptions as $key => $val) {
    
        // You can access the description like this
        $val
        // or this
        $descriptions[$key]
    
        // so naturally you can access the image like this:
        $images[$key]
    
    }
    

    您也可以检查描述/图像的计数,并将其用作循环的计数器:

    $descriptions = $request->get('description');
    $images = $request->file('image'); // assign array of images
    
    $descCount = count($descriptions);
    $imgCount = count($images);
    

    但是,如果您可能有不同的布局,即您没有1个图像的1个描述,请澄清,因为您需要采取不同的方法 .

相关问题