来自数据库laravel 5.6的图像

时间:2018-05-23 17:38:49

标签: database laravel image

这是edit.blade.php

  {{ Form::model($post,['route'=>['posts.update',$post->id],'method'=>'PATCH']) }}
  <div class="row">
      <div class="col-sm-2">
        {!! form::label('title','Title') !!}
      </div>
      <div class="col-sm-10">
        <div class="form-group {{ $errors->has('title') ? 'has-error' : "" }}">
          {{ Form::text('title',NULL, ['class'=>'form-control', 'id'=>'title', 'placeholder'=>'Title Post...']) }}
          {{ $errors->first('title', '<p class="help-block">:message</p>') }}
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-2">
        {!! form::label('descrption','Description') !!}
      </div>
      <div class="col-sm-10">
        <div class="form-group {{ $errors->has('descrption') ? 'has-error' : "" }}">
          {{ Form::text('descrption',NULL, ['class'=>'form-control', 'id'=>'descrption', 'placeholder'=>'Description Post...']) }}
          {{ $errors->first('descrption', '<p class="help-block">:message</p>') }}
        </div>
      </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12">
      <div class="form-group">
          <strong>Image:</strong>
         {!! Form::file('image',array('placeholder' => 'Image','class' => 'form-control','onchange' => 'document.getElementById("blah").src = window.URL.createObjectURL(this.files[0])')) !!}
      </div>
      <img id="blah" src="{{URL::to('/')}}/uploads/{{$post->image}}" alt="your image" width="150"/>
  </div>
  <div class="col-xs-12 col-sm-12 col-md-12 text-center">
          <button type="submit" class="btn btn-primary">Submit</button>
  </div>

  {{ Form::close() }}
</div>

@endsection

这是控制器

class PostController extends Controller

{     / **      *显示资源列表。      *      * @return \ Illuminate \ Http \ Response      * /     公共职能指数()     {         $ post = Post :: paginate(5);

    return view('admin.posts.index', compact('post');
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('admin.posts.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{  
    $this->validate($request, [
        'title' => 'required|string|max:255', 
        'descrption' => 'required',
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $image = $request->file('image');         
    $fileName = $image->getClientOriginalName();
    $fileExtension = $image->getClientOriginalExtension();
    // $imageName = date('dmY').'.'.$request->file('image')->getClientOriginalExtension();
    $request->file('image')->move(
    base_path() . '/public/uploads/', $fileName);
    $requestData = $request->all();
    $requestData['image'] = $fileName;
    Post::create($requestData);
        Post::create($request->all());
        return redirect()->route('posts.index')->with('succes', 'Post created succes');

}


public function show($id)
{

    return view('admin.posts.show');

}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
   $post= Pots::find($id);

    return view('admin.posts.edit', compact('post'));
}
public function update(Request $request, $id)
{
    $this->validate($request, [
        'title' => 'required',

        'descrption' => 'required',
    ]);

    $image = $request->file('image ');         
    $fileName = $image ->getClientOriginalName();
    $fileExtension = $image ->getClientOriginalExtension();
    // $imageName = date('dmY').'.'.$request->file('image')->getClientOriginalExtension();
    $request->file('image ')->move(
    base_path() . '/public/uploads/', $fileName);
    $requestData = $request->all();
    $requestData['image '] = $fileName;
    Post::find($id)->update($requestData);


    Post::find($id)->update($request->all());
    return redirect()->route('posts.index')->with('succes', 'Post was updated');
}

public function destroy($id)
{
    Post::find($id)->delete();
    return redirect()->route('posts.index');
}

}

当我想编辑帖子时我有这个错误: 在null上调用成员函数getClientOriginalName() 而且我不知道为什么......

1 个答案:

答案 0 :(得分:0)

https://laravelcollective.com/docs/master/html#opening-a-form

  

如果您的表单要接受文件上传,请添加文件选项   你的阵列:

     

echo Form :: open(['url'=&gt;'foo / bar','files'=&gt; true])