>我正在尝试从表单中获取输入并将其保存到数据库中。该表单包括图片上传。
当我尝试下面的代码时(我知道这都是错误的(,我得到"在数组上调用成员函数 save(("。我不确定如何修改代码以使其正确
控制器
public function store(Request $request){
//
$input = $request->all();
$input->save();
if($file = $request->file('image')){
$name = $file->getClientOriginalName();
if($file->move('image', $name)){
$post = new Gallery();
$post->image = $name;
$post->save();
return redirect()->route('admin.gallery.index');
};
};
}
创建边栏选项卡
@extends('layouts.admin')
@section('content')
<h1>UPLOAD PICTURES</h1>
{!! Form::open(['method' =>'POST', 'action'=> 'GalleryController@store',
'files'=>true, 'enctype'=>'multipart/form-data']) !!}
<div class="form-group">
{!! Form::label('Picture:') !!}
{!! Form::file('image', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('species_id', 'Species:') !!}
{!! Form::select('species_id', [''=>'Choose Species'] + $species, null,
['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('name', 'Image Title:') !!}
{!! Form::text('name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('patreon', 'Patreon Image?:') !!}
{!! Form::select('patreon', array(1 =>'Yes', 0=>'No'), 0,['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Upload Image', ['class'=>'btn btn-primary']) !!}
@endsection
型
namespace App;
use IlluminateDatabaseEloquentModel;
class Gallery extends Model
{
//
protected $fillable = [
'image',
'species_id',
'name',
'tag',
'patreon'
];
public function species(){
return $this->belongsTo('AppSpecies');
}
}
整个问题就在这个代码块中。
$input = $request->all();
$input->save();
$request->all();
返回来自请求的输入数组。现在$input
是一个数组,为什么你试图在数组上调用->save();
,我不知道,但如果你删除那行它会起作用。
它说你得到了你的对象,但在数组中
$foo =
[ 0 => yourObject ]
如果您只有一件物品,只需执行
$foo = end($foo)
如果结果数组中有多个项目,则 jus 通过 foreach 循环迭代它