laravel Connection.php 第 647 行中的 QueryException:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43042036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QueryException in Connection.php line 647:
提问by Isis
I'm trying to save checkboxes. I should be able to select one or mutliple. At the moment I'm getting this error
我正在尝试保存复选框。我应该可以选择一个或多个。目前我收到此错误
QueryException in Connection.php line 647: Array to string conversion (SQL: insert into
seo
(url
,meta_title
,meta_description
,keywords
,robot
,updated_at
,created_at
) values (ff, ff, ff, ff, noindex, 2017-03-27 08:59:54, 2017-03-27 08:59:54))
Connection.php 第 647 行中的 QueryException:数组到字符串的转换(SQL:插入到
seo
(url
,meta_title
,meta_description
,keywords
,robot
,updated_at
,created_at
) values (ff, ff, ff, ff, noindex, 2017-03-27 08:59:54, 2017- 03-27 08:59:54))
Here is my create.blade.php
这是我的 create.blade.php
<div class="robots">
{!! Form::label('noindex', 'noindex') !!}
{!! Form::checkbox('robot[]', 'noindex') !!}
{!! Form::label('follow', 'follow') !!}
{!! Form::checkbox('robot[]', 'follow') !!}
</div>
my method in my seo controller
我的 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'));
}
}
回答by Alexey Mezenin
You've trying to save robot
array as string. You need to serialize it first. You can do it manually:
您试图将robot
数组保存为字符串。您需要先对其进行序列化。您可以手动完成:
$robot = json_encode($input['robot']);
Or you can use array castingfeature:
或者您可以使用数组转换功能:
protected $casts = [
'robot' => 'array',
];
The array cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a JSON or TEXT field type that contains serialized JSON, adding the array cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model:
在处理存储为序列化 JSON 的列时,数组转换类型特别有用。例如,如果您的数据库有一个包含序列化 JSON 的 JSON 或 TEXT 字段类型,当您在 Eloquent 模型上访问它时,将数组转换添加到该属性将自动将属性反序列化为 PHP 数组: