我试图保存复选框。我应该能够选择一个或多个。 目前我收到此错误
Connection.php第647行中的QueryException:数组转换为字符串(SQL:插入
seo
url
,meta_title
,meta_description
,keywords
,robot
,updated_at
,created_at
)值(ff,ff,ff,ff,noindex,2017-03-27 08:59:54,2017-03-27 08:59: 54))
这是我的create.blade.php
<div class="robots">
{!! Form::label('noindex', 'noindex') !!}
{!! Form::checkbox('robot[]', 'noindex') !!}
{!! Form::label('follow', 'follow') !!}
{!! Form::checkbox('robot[]', 'follow') !!}
</div>
我的seo控制器中的方法
public function create()
{
//Gets all the seo that are in the database
$seos = Seo::with('menu')->get();
//Lists the title of the seo
$menu_options = Menu::pluck('title', 'id');
return view('seo::admin.create', compact('seos'))->with('menu_options', $menu_options);
}
public function store()
{
//Gets all the input in the fields in the form
$input = Input::all();
//Checks the input fields against the validation rules in the Seo model
$validation = Validator::make($input, Seo::$rules);
//If the validation fails the a message will pop up saying that there was validation errors
if($validation->fails()){
return redirect()->route('seo.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors');
}
//If the validation passes then it gets saved to the database
if($validation->passes()){
$seos = new Seo();
//Gets the menu_id
$menuId = (array) array_get($input, 'menu_id');
//Saves the input to the database
$seos->fill($input)->save();
//Syncs the menu function in the Seo model to save the menu ID in menu_seo
$seos->menu()->sync($menuId);
$seos = Seo::all();
return view('seo::admin.index', compact('seos'));
}
}
答案 0 :(得分:0)
您尝试将robot
数组保存为字符串。您需要先将其序列化。你可以手动完成:
$robot = json_encode($input['robot']);
或者您可以使用array casting功能:
protected $casts = [
'robot' => 'array',
];
在处理存储为序列化JSON的列时,数组强制转换类型特别有用。例如,如果您的数据库具有包含序列化JSON的JSON或TEXT字段类型,则在您的Eloquent模型上访问该属性时,将该属性添加到该属性将自动将该属性反序列化为PHP数组: