Laravel:创建表单显示照片并编辑不显示照片的表单

时间:2017-02-27 06:33:44

标签: php html5 laravel laravel-5.3 laravel-5.4

创建表单中的dd($ input)显示以下内容:

array:5 [▼
  "_token" => "Q6xauvlFx0k61JDqFvo4FsJtr5Y9EbVX8V3Wwk8V"
  "blockquote" => "test block quote content"
  "name" => "test name"
  "description" => "test description"
  "photo" => UploadedFile {#267 ▶}
]

但编辑表单的dd($ input)显示以下内容

array:9 [▼
  "_method" => "PATCH"
  "_token" => "oDzfMvXiWMUzAr6zftnTZb9IVQCwWa2Tt2ZPBp6x"
  "photo" => "43-356x220.jpg"
  "blockquote" => "asf"
  "name" => "Af"
  "description" => "af"
  "files" => "1"
  "enctype" => "multipart/form-data"
  "data-toggle" => "validator"
]

因此导致图片上传失败并

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

上面的行返回null。我想要照片,我应该怎么做。

我认为这在创建方法'POST'上工作,并在编辑方法'PATCH'中显示不同。如何在编辑中实现?

编辑:我的代码已添加

create.blade.php code

 {!! Form::open([
        'route' => 'MessageFromAlumniPresident.store', 'id' => 'formidda', 'files' => true, 'enctype' => 'multipart/form-data', 'data-toggle'=>'validator'
     ]) !!}

    @include('Admin.MessageFromAlumniPresident.create_edit_common')

    <button class="btn btn-success" type="submit">Submit</button>


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

控制器:

public function store(Request $request)
    {
$this->validate($request, [
            'blockquote' => 'required',
            'name' => 'required',
            'description' => 'required',
            'photo' => 'required'
        ]);

        $input = $request->all();


        $hi = MessageFromAlumniPresident::create($input);

        //applicant photo
        $image = Input::file('photo');
        if($image === NULL){
            Session::flash('flash_message', 'Product Master successfully added!');

            //TODO: redirect to Party list page
            return redirect()->route('MessageFromAlumniPresident.index');
        }else{

            $filename = 'alumni_president'. '.' . $image->getClientOriginalExtension();
            $path = public_path('/images/MessageFromAlumniPresident/'.$filename);
            Image::make($image->getRealPath())->save($path);
        }

此创建工作正常......

edit.blade.php

 {!! Form::model($item, [
      'route' => ['MessageFromAlumniPresident.update', $item->id, 'files' => true, 'enctype' => 'multipart/form-data', 'data-toggle'=>'validator']
      ]) !!}

    @include('Admin.MessageFromAlumniPresident.create_edit_common')

    <button class="btn btn-success" type="submit">Submit</button>
    {!! Form::close() !!}

控制器:对于更新,这对照片不起作用

public function update(Request $request, $id)
    {
        $items = MessageFromAlumniPresident::findOrFail($id);

        $this->validate($request, [
            'blockquote' => 'required',
            'name' => 'required',
            'description' => 'required',
            'photo' => 'required'
        ]);

        $input = $request->all();


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

        $items->fill($input)->save();

        if($image === NULL){
            Session::flash('flash_message', 'Product Master successfully added!');

            //TODO: redirect to Party list page
            return redirect()->route('MessageFromAlumniPresident.index');
        }else{
            $path = public_path('/images/MessageFromAlumniPresident/alumni_president.jpg');

            \File::delete($path);

            $filename = 'alumni_president'. '.' . $image->getClientOriginalExtension();
            $path = public_path('/images/MessageFromAlumniPresident/'.$filename);
            Image::make($image->getRealPath())->save($path);
        }

0 个答案:

没有答案