【发布时间】:2018-04-08 19:25:39
【问题描述】:
我想通过 API 在我的数据库中创建一条新记录,但我收到错误“消息”:“来自 Postman 的数组到字符串转换(SQL:插入....)
API 路由:
Route::post('/posts','PostController@store');
我的控制器中的存储功能:
public function store(Request $request)
{
$post= new Post;
$post->all = $request->all();
$post->save();
}
【问题讨论】:
-
all是什么类型的?它是一个数据库字段,或者您想以这种方式将所有请求字段设置为模型属性?如果你想设置请求中的所有字段 - 你需要$post->fill($request->all); -
$post->all 只是 post 的一个属性(代表一个列)。您不能将请求中的数组分配给它。
-
我该怎么做?
-
为什么不使用普通列作为值?
-
但请记住,
fillable属性必须具有用于批量分配的字段数组。更多详情请参考laravel.com/docs/5.5/eloquent - Mass Assignment