2014-10-06 113 views
1

我创造了像在我看来图片上传空上Laravel 5.0

{!! Form::open(array('url' => 'user/posts', 'files'=> true)) !!} 
      <div class="form-group"> 
       {!! Form::text('title',Input::old('title'),['class'=>'form-control', 'placeholder' => 'What is your title?']) !!} 
      </div> 
      <div class="form-group"> 
       {!! Form::label('image', 'Your Image') !!} 
       {!! Form::file('image') !!} 
      <p class="help-block">Hey! Please don't upload over 15MB images!</p> 
      </div> 
      <div class="form-group"> 
      {!! Form::label('caption', 'Don\'t miss to caption!') !!} 
      {!! Form::text('caption',Input::old('caption'),['class'=>'form-control', 'placeholder' => 'Your caption']) !!} 
      </div> 
      {!! Form::submit('Post now!',['class'=>'btn btn-info']) !!} 
     {!!Form::close() !!} 

以下一种形式,我尝试DD()为我的形象从我的控制器,但它返回nulll。下面一个是我Controller

public function store(PostFormRequest $request) 
    { 
    dd($request->input('image')); 
    $post = new Post; 
    } 

下一个是我PostFormRequest

<?php namespace App\Http\Requests; 

use Response; 
use Illuminate\Foundation\Http\FormRequest; 

class PostFormRequest extends FormRequest { 

    public function rules() 
    { 
     return [ 
      'title' => 'required', 
      'image' => 'mimes:jpeg,bmp,png' 
     ]; 
    } 

    public function authorize() 
    { 
    $image = $this->image; 
    if(empty($image)) 
     return true; 
    else 
     return false; 
    } 

    public function forbiddenResponse() 
    { 
     return Response::make('Sorry!',403); 
    } 
} 

当我尝试dd($request->input('title'));这是返回string(4) "test"是我错了?

回答

6

对于文件,你需要使用:

dd($request->file('image')); 

,而不是

dd($request->input('image')); 

Reference

1

同样的问题面对。还有一件小事:如果你使用原生HTML表单提交,你还需要设置'enctype ='multipart/form-data''。否则$ request-> file('INPUT_NAME')将返回字符串,而不是文件。