我想用这些代码将数据保存到表中
创建.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>
城市控制器.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);
}
但是当我在create视图中按下submit按钮时
errorexception(e\u警告)尝试分配非对象的属性'city\u name'
我怎样才能解决这个问题?
1条答案
按热度按时间qmelpv7a1#
不能将数组与对象访问器一起使用。在控制器中,它应该是: