错误:
Attempt to assign property of non-object; ErrorException in AdminController.php line 40:
AdminController:
public function createSlider(AdminRequest $request)
{
$input = Request::all();
Sliders::create($input);
if (Request::hasFile('image')) {
$imageName = Request::input('title'). '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName; //------------> line 40.......
}
$input->save();
}
HTML
{!!Form::open(array('url' => 'admin/new_slider', 'files' => true)) !!}
<div class = "form-group">
{!!Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!!Form::text('title', null, ['class'=> 'input-mini ina tch'])!!}
{!!Form::label('title', 'Description:', ['class' => 'control-label']) !!}
{!!Form::text('description', null, ['class'=> 'input-mini '])!!}
</div>
<div class = "form-group">
{!!Form::label('title', 'Link:', ['class' => 'control-label']) !!}
{!!Form::text('link', null, ['class'=> 'input-mini'])!!}
{!!Form::label('title', 'Image:', ['class' => 'control-label']) !!}
{!! Form::file('image', ['id' => 'imgInp', 'class' => 'prev-upload']) !!}
</div>
<div class = "form-group">
{!!Form::submit('Submit', ['class'=> 'btn btn-default'])!!}
</div>
{!! Form :: close()!!}
我整个上午一直在努力解决这个问题。我希望能够接受文件上传以及表单信息。重命名文件不是我认为我可以让它工作的方式。有没有更好的方法来执行此文件上传和移动?
答案 0 :(得分:1)
我改变了所有内容的格式并且有效。
public function createSlider(AdminRequest $request)
{
$slider = new Sliders(array(
'title' => $request->get('title'),
'description' => $request->get('description'),
'link' => $request->get('link')
));
$slider->save();
$imageName = $slider->title .'_gin_slider'. '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image', $imageName
);
$slider->image = $imageName;
$slider->save();
return redirect('/admin');
}
答案 1 :(得分:0)
以供进一步参考:当您的控制器,路由器或中间件方法未返回有效响应时,会发生此问题。无论是实际数据还是重定向,您都应该始终从根调用的方法返回响应。对于中间件,可以通过Closure本身作为请求。
对于控制器,应为:
public function index()
{
return response() || redirect()->back();
}
如果是中间件:
public function handle($request, Closure $next)
{
return $next($request) || response() || redirect()->back();
}
如果路线关闭:
Route::get('foo/bar', function(){
return response() || redirect()->back();
});
||
的意思是“或”