【问题标题】:Cannot update file upload form, laravel form binding无法更新文件上传表单,laravel 表单绑定
【发布时间】:2015-02-13 17:32:36
【问题描述】:

您好,我必须通过文件上传来编辑和更新表单。当我编辑其他字段并上传照片时,它可以工作。但如果我在不上传照片的情况下编辑其他字段,则会显示错误“在非对象上调用成员函数 getClientOriginalName()”

我的看法

{{ Form::model($layer, array('method'=>'PATCH', 'route'=>array('edit-layer-patch', $layer->id), 'files'=> true)) }}


<div class="form-group">
    <input type="file" name="image" value="{{ $layer->img_path }}" class="form-control" onchange="readURL(this);" id="image" accept="image/*" />
    {{ HTML::image('uploads/'. $layer->img_path, 'your image', array('id' => 'blah', 'width'=>150, 'height' => 100)) }}
</div>

<div class="form-group">
    <label for="layer_name">Layer Name</label>
    <input type="text" class="form-control"  name="layer_name" value="{{ $layer->layer_name }}" />
</div>

<div class="form-group">    
<label for="select_user">Select User</label>
<select name="select_user" class="form-control">
    <option value="{{ $layer->user->id }}">{{ $layer->user->firstname }}</option>
    @foreach($users as $user)
    <option value="{{ $user->id }}">{{ $user->firstname }}</option>
    @endforeach
</select>
</div>

<div class="form-group">    
<label for="select_style">Select Style</label>
<select name="select_style" class="form-control">
    <option value="{{ $layer->style->id }}">{{ $layer->style->style_name }}</option>
    @foreach($styles as $style)
    <option value="{{ $style->id }}">{{ $style->style_name }}</option>
    @endforeach
</select>
</div>

<div class="form-group">    
<label for="select_category">Select Category</label>
<select name="select_category" class="form-control">
    @if(!empty($layer->category->category_name))
    <option value="{{ $layer->category->id }}">{{ $layer->category->category_name }}</option>
    @foreach($categories as $category)
    <option value="{{ $category->id }}">{{ $category->category_name }}</option>
    @endforeach
    @else
    <option value="">Deleted</option>
    @foreach($categories as $category)
    <option value="{{ $category->id }}">{{ $category->category_name }}</option>
    @endforeach
    @endif

</select>
</div>

<div class="form-group">
    <label for="published">Published</label>
    <input type="number" class="form-control" min="0" max="1"  name="published" value="{{ $layer->published }}" />
</div>

<div class="form-group">
    <label for="default">Default</label>
    <input type="number" class="form-control" min="0" max="1"  name="default" value="{{ $layer->default }}" />
</div>

<div class="form-group">
    <button class="btn btn-primary" type="submit">Update</button>
    <a class="btn btn-danger" href="{{ route('layers') }}">Cancel</a>
</div>

{{ Form::close() }}

我的控制器

public function edit($id){

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

    $filename = $image->getClientOriginalName();

    $destinationPath = 'uploads/';

    $layer = Layer::find($id);

    $validator = Validator::make(Input::all(), Layer::$rules);

    if ($validator->fails()) {

        return Redirect::route('edit-layer')->withErrors($validator)->with('message', 'Ups something happened');
    }else{  
     //Image::make($image->getRealPath())->save($path);
        Input::file('image')->move($destinationPath, $filename);



        if(Input::has('image')){
            $layer->user_id       = Input::get('select_user');
            $layer->style_id      = Input::get('select_style');
            $layer->category_id   = Input::get('select_category');
            $layer->layer_name    = Input::get('layer_name');
            $layer->published     = Input::get('published');
            $layer->default       = Input::get('default');
            $layer->save(); 
        }else{
            $layer->user_id       = Input::get('select_user');
            $layer->style_id      = Input::get('select_style');
            $layer->category_id   = Input::get('select_category');
            $layer->layer_name    = Input::get('layer_name');
            $layer->published     = Input::get('published');
            $layer->default       = Input::get('default');
            $layer->img_path = $filename;
            $layer->save(); 
        }

        return Redirect::route('layers')->with('message','Layer Updated');
    }
}

请帮帮我,谢谢

【问题讨论】:

    标签: php forms laravel


    【解决方案1】:

    因为你这样做:

    $image = Input::file('image');
    $filename = $image->getClientOriginalName();
    

    即使你不上传图片(ergo $imagenull

    您需要将所有与图片相关的内容移到if(Input::has('image')) 支票中

    public function edit($id){
    
        $destinationPath = 'uploads/';
        $layer = Layer::find($id);
        $validator = Validator::make(Input::all(), Layer::$rules);
    
        if ($validator->fails()) {
            return Redirect::route('edit-layer')->withErrors($validator)->with('message', 'Ups something happened');
        }else{  
    
            if(Input::has('image')){
                $image = Input::file('image');
    
                $filename = $image->getClientOriginalName();
    
                Input::file('image')->move($destinationPath, $filename);
    
                // ...
            }else{
                // ...
            }
    
            return Redirect::route('layers')->with('message','Layer Updated');
        }
    }
    

    更新

    来自 cmets

    admin/layers/%7Bid%7D/edit 实际上是admin/layers/{id}/edit

    看起来验证失败并转到Redirect::route('edit-layer') 缺少 id 参数

    这样做:

    return Redirect::route('edit-layer', array($id))->withErrors($validator)->with('message', 'Ups something happened');
    

    【讨论】:

    • 我这样做了,但它仍然显示“Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException”
    • 嗯,这可能是一个新问题。重定向是否发生或之前是否显示错误?
    • 当我点击提交时,它也会发生重定向。它从更新中添加了一些百分比不同的 url(当我上传照片时) admin/layers/%7Bid%7D/edit
    • 谢谢,我得到了解决方案。错误是当我验证表单时。我删除了验证,它可以工作。它会在上传和不上传照片的情况下更新表单。非常感谢。我只有 13 名声望,我不能支持你的回答
    • 我修复了错误。验证向我显示错误,因为我需要输入文件,这就是原因。 (捂脸)
    【解决方案2】:

    您需要在 if(Input::has('image')) 语句中移动所有图像内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多