Php Laravel:使用干预在foreach循环中上载带有图像的表单数据

Php Laravel:使用干预在foreach循环中上载带有图像的表单数据,php,laravel,Php,Laravel,我使用的是Laravel5.1,希望有一个表单,其中包含几行文本字段及其相应的图像上传 表格: 我的问题是,我如何使用带有$key值的干预在表中保存一个新行,该行带有与图像上载相关的文本描述,并且仍然使用所有干预类?我的密码接近了吗 我可以很容易地用一个表单输入和一个图像上传来完成这一切,但我的目标是拥有一个包含多行输入的页面。谢谢 我不确定您是如何管理多个字段的,不管它是固定不变的还是可以动态更改的。我以前使用javascript进行过动态方法,并记录了使用隐藏文本字段作为计数所需的动态字段数

我使用的是Laravel5.1,希望有一个表单,其中包含几行文本字段及其相应的图像上传

表格:

我的问题是,我如何使用带有
$key
值的干预在表中保存一个新行,该行带有与图像上载相关的文本描述,并且仍然使用所有干预类?我的密码接近了吗


我可以很容易地用一个表单输入和一个图像上传来完成这一切,但我的目标是拥有一个包含多行输入的页面。谢谢

我不确定您是如何管理多个字段的,不管它是固定不变的还是可以动态更改的。我以前使用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>
"imageAndDescriptionCount" => "2"
"description" => array:2 [▼
    0 => "description"
    1 => "description"
]
    "image" => array:2 [▼
    0 => UploadedFile {#154 ▶}
    1 => UploadedFile {#155 ▶}
]
这样,您就可以在控制器中很好地设置for循环:

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');
    }
}
$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');
您也可以检查描述/图像的计数,并将其用作计数器来循环:

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

$descCount = count($descriptions);
$imgCount = count($images);

但是,如果您可能有不同的布局,即您没有一张图片的一个描述,那么请澄清,因为您需要采取不同的方法。

什么是干预?@haakym感谢您的全面回答。虽然您的响应符合逻辑,但唯一的问题是,在html表单上,我使用了“添加另一个图像”克隆
表tr
的按钮功能,因此从控制器端看,带有描述的图像数量未知,因此我尝试使用foreach循环,而不是像您所建议的那样进行硬计数。我不是说对其进行硬编码。动态使用计数器值,在添加(单击“添加另一个图像”按钮函数)或使用javascript删除行时更新其值。我假设每次你添加一行,你都会添加一个额外的描述和一个额外的图像,它们是成对的,对吗?因此,我的方法应该可以很好地工作,您只需添加代码来更新计数器值。这是真的。让我来试一试,祝你一切顺利。如果你在某件事上卡住了,一定要更新你的问题。
$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);