我正在尝试上传一些图片,但我不知道自己做错了什么:(
{{ Form::open (['route' => 'titulos.store', 'class'=> 'form', 'files'=> 'true']) }}
{{ Form::label('title', "Titulo:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::text('title') }}
{{ $errors->first('title') }}
<div class="form-group">
{{ Form::label('date', "Fecha:", ['class' => 'col-sm-2 control-label']) }}
<input type="date" name="date" >
</div>
{{ Form::label('description', "Description:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::textarea('description') }}
{{ $errors->first('description') }}
<div class="form-group">
{{ Form::file('image') }}
</div>
{{ Form::label('category_id', 'Category:', ['class' => 'col-sm-2 control-label']) }}
<div class="col-sm-10">
{{ Form::select('category_id', array('1' => 'TBLeaks', '2' => 'Quejas', '3' => 'Denuncias', '4' => 'Ideas'), null, array('class' => 'form-control')) }}
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
{{ Form::submit('Submit', ['class' => "btn btn-primary"]) }}
</div>
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<a class="btn btn-success" href="{{ URL::to('admin') }}">Back to Admin</a>
</div>
</div>
{{ Form::close() }}
</div>
@if(Session::has('message'))
<div class="alert alert-{{ Session::get('class') }}">{{ Session::get('message')}}</div>
@endif
@stop
类TitulosController扩展了BaseController {
public function store(){
$rules = array(
'title' => 'required',
'description' => 'required',
'category_id' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
// proceso de valicion
if ($validator->fails()) {
return Redirect::to('titulos/create')
->withErrors($validator)
->withInput()->withErrors($validator);
} else {
//store
$image = Input::file('image');
$filename = $image->getClientOriginalName();
if(Input::hasFile('image')){
Input::file('image')->move(public_path().'/assets/img/', $filename);
}
$titulo = new Titulo();
$titulo->id = Input::get('id');
$titulo->title = Input::get('title');
$titulo->description = Input::get('description');
$titulo->date = Input::get('date');
$titulo->image = Input::$filename('image');
$titulo->category_id = Input::get('category_id');
$titulo->save();
return Redirect::to('titulos');
}
use SoftDeletingTrait; // for soft delete
protected $dates = ['deleted_at']; // for soft delete
protected $table = 'titulos';
protected $primaryKey = 'id';
protected $fillable = array('image');
public $timestamps = true;
public function __construct() {
parent::__construct();
}
public function category(){
return $this->belongsTo('Category');
}
}
class Image扩展了Eloquent {
public $timestamps = false;
protected $fillable = array('image');
}
答案 0 :(得分:0)
在此行,您的商店到公共路径应该没问题
if(Input::hasFile('image')){
Input::file('image')->move(public_path().'/assets/img/', $filename);
}
只有您的保存Tutilo对象看起来不太好。我认为您错误地在此$
上添加了Input::filename
。这将调用图像
$titulo->image = Input::$filename('image');
将您的代码更改为此
$titulo = new Titulo();
$titulo->id = Input::get('id');
$titulo->title = Input::get('title');
$titulo->description = Input::get('description');
$titulo->date = Input::get('date');
$titulo->image = $filename; // I change this line. assuming you want to store the file name
$titulo->category_id = Input::get('category_id');