首页 > 解决方案 > 从动态添加的文件上传器 Laravel 将图像保存到数据库

问题描述

我有一个表,我在其中使用 JS 动态添加行,效果很好,我也可以使用 for 循环将这些文本字段添加到 db,现在其中一行必须是图像上传器,通过当前代码我是只能保存一张图片,其他不同行的图片不保存,请帮忙。谢谢你。

刀片文件:

<table class="table table-striped">
    <thead>
        <tr>
            <th></th>
            <th>Name</th>
            <th>Price</th>
            <th>Tags</th>
            <th>Image</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type="button" class="btn-link cursor-pointer" value="Add Menu Item" href="javascript:void(0);">
                <input type="hidden" class="tbl_row" name="count_row[]" id="count_row[]" value="1"/>
            </td>
            <td>
                {{ Form::text('item_name[]', null, ['id' => 'name', 'class' => 'form-control'] ) }}
            </td>
            <td>
                {{ Form::text('price[]', null, ['id' => 'price', 'class' => 'form-control'] )}}
            </td>
            <td>
                {{ Form::select('tags[]', $tags, null, ['class' => 'form-control']) }}
            </td>
            <td>
                <input type="file" name="image[]">
            </td>
            <td>
                {{ Form::textarea('description[]', null, ['id' => 'description', 'rows' => 2, 'class' => 'form-control']) }}
            </td>
        </tr>
    </tbody>
</table>

控制器:

$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');

for ($ln = $start_count; $ln < $end_count; $ln++) {
    if (isset($attachment[$i]) && is_file($attachment[$i])) {
        $fileNameWithExt = $attachment[$i]->getClientOriginalName();
        // Get just filename
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        // Get just ext
        $extention = $attachment[$i]->getClientOriginalExtension();
        // Filename to store
        $fileNameToStore = $filename . $extention;
        // Upload Image
        $path = $attachment[$i]->storeAs('public/content-images/', $fileNameToStore);
    } else {
        $fileNameToStore = 'no-image.jpg';
    }

    $item = new MenuItem([
        'category_id' => $category->id,
        'name' => $request->item_name[$ln],
        'price' => $request->price[$ln],
        'description' => $request->description[$ln],
        'status' => 1,
        'image' => $fileNameToStore[$ln], **even the name is not saving properly in db**
    ]);
    $item->save();
    DB::table('menu_item_tag')->insert([
        ['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
    ]);
}

标签: laravellaravel-5.5

解决方案


感谢 azeós 的评论设法通过将代码更改为此来解决,必须将 $i 更改为 $ln 并将 fileNameToStore[$ln] 更改为 fileNameToStore 才能将正确的文件名保存到 db:

            $start_count = (isset($end_count)) ? $end_count : 0;
            $end_count = (int)$start_count + (int)$request->input('count_row')[$i];
            $attachment = $request->file('image');

            for ($ln = $start_count; $ln < $end_count; $ln++) {
                if (isset($attachment[$ln]) && is_file($attachment[$ln])) {
                    $fileNameWithExt = $attachment[$ln]->getClientOriginalName();
                    // Get just filename
                    $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
                    // Get just ext
                    $extention = $attachment[$ln]->getClientOriginalExtension();
                    // Filename to store
                    $fileNameToStore = $filename . '.' . $extention;
                    // Upload Image
                    $path = $attachment[$ln]->storeAs('public/menu/'.$category->id.'/', $fileNameToStore);
                } else {
                    $fileNameToStore = 'no-image.jpg';
                }

                $item = new MenuItem([
                    'category_id' => $category->id,
                    'name' => $request->item_name[$ln],
                    'price' => $request->price[$ln],
                    'description' => $request->description[$ln],
                    'status' => 1,
                    'image' => 'menu/' . $category->id . '/' . $fileNameToStore,
                ]);
                $item->save();
                DB::table('menu_item_tag')->insert([
                    ['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
                ]);
            }

推荐阅读