我想使用这些代码将数据保存到表中
create.blade.php
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="Name"> City Name:</label>
<input type="text" class="form-control" name="city_name">
</div>
</div>
CityController.php
public function store(Request $request)
{
$options = array();
$options->city_name=$request->get('city_name');
$attributes =array('city_name');
$table = 'cities';
(new City())->store($table,$attributes,$options);
return redirect('cities')->with('success','Information has been inserted');
}
我的模特是 City.php
public function store($table,$attributes,$options)
{
(new Root())->store($table,$attributes,$options);
}
具有根模型 Root.php
public function store($table ,$attributes, $options) {
$value = array();
foreach($attributes as $key=>$data)
{
$value[$data] = $options[$key];
}
DB::table($table)->insert($value);
}
但是当我在创建视图中按下提交按钮时,给我
ErrorException(E_WARNING) 尝试分配非对象的属性“ city_name”
我该如何解决?
答案 0 :(得分:0)
您不能将数组与对象访问器一起使用。在您的控制器中,应该为:
$options = array();
$options['city_name'] = $request->get('city_name');
$attributes = array('city_name');