2014-10-06 124 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')將返回字符串,而不是文件。