在更新 Laravel 中上传图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34961942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:05:21  来源:igfitidea点击:

Upload image in update Laravel

phplaravellaravel-5

提问by Pierangelo Orizio

in Laravel5 I can't update image in Edit, when I upload new file, this result null... Below my code in controller, routes and edit view.

在 Laravel5 中,我无法在编辑中更新图像,当我上传新文件时,此结果为空...在控制器、路由和编辑视图中的代码下方。

here my controller:

这是我的控制器:

public function update(Request $request, $id)
{
    $slider = Slider::findOrFail($id);
    //$input = $request->all();

    // uploads image
    if ($request->hasFile('image'))
    {
        $file = $request->file('image');
        $imagename = 'dpgomma_' . time() . '-' . $file->getClientOriginalName();
        $file->move(public_path().'/uploads/', $imagename);
        $input['image'] = $imagename;
    }
    $slider->save($request->all());
    return redirect('/admin/sliders')->with('message', 'Slider Modificata');
}

my routes:

我的路线:

Route::get('/admin/sliders/edit/{id}', 'SliderController@edit');
Route::put('/admin/sliders/update/{id}',['as' => 'sliders.update', 'uses' => 'SliderController@update']);

My view form:

我的视图形式:

{!! Form::model($slider, [
'method' => 'put',
'route' => ['sliders.update', $slider->id],
'files' => true
]) !!}

{!! Form::token() !!}

<div class="form-group">
    {!! Form::label('title', 'Title:', ['class' => 'control-label']) !!}
    {!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
    {!! Form::label('image', 'image:', ['class' => 'control-label']) !!}
    {!! Form::file('image', null, ['class' => 'form-control']) !!}
</div>

{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}

{!! Form::close() !!}

回答by PeterPan666

You have to add the 'files' => trueoption in your Form opening.

您必须'files' => true在表单打开中添加该选项。

Also, you can use $request->hasFile('image')and $request->file('image')instead of the Inputfacade.

此外,您可以使用$request->hasFile('image')and$request->file('image')代替Input外观。

I suggest you to take a look at this, there's no more reason not to work.

我建议你看看这个,没有理由不工作了。

回答by smartrahat

Your database is not updating image name because the image name contains in input['image']and you updating your database with $requestservice container. So, the original name of your image going through $request service container like

您的数据库没有更新图像名称,因为图像名称包含在input['image']并且您使用$request服务容器更新数据库。因此,您的图像的原始名称通过 $request 服务容器,例如

$request = [
    'other' => 'other',
    'image' => 'you_original_image_name',
    'more_other' => 'more_other'
]

To update the database with desire file name change this line

要使用所需的文件名更新数据库,请更改此行

$input['image'] = $imagename;

To

$request['image'] = $imagename;