我在视图中创建了一个类似下面的表单
{!! 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"
我错了什么?
答案 0 :(得分:6)
答案 1 :(得分:1)
面临同样的问题。还有一点:如果您使用原生HTML表单提交,您还需要设置&#39; enctype =&#34; multipart / form-data&#34;&#39;。否则$ request-&gt;文件(&#39; INPUT_NAME&#39;)会返回字符串而不是文件。