我正在尝试存储记录的用户id,但我收到了这个错误
ErrorException
array_map(): Argument #2 should be an array
这是控制器中的代码
public function store(Request $request)
{
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$data = request()->validate([
'id' => $id = Auth::id(),
'content' => 'required',
'topic' => 'required',
'hashtag' => 'required'
]);
$check = Tweets::create($data);
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}
错误在这行代码中。
'id' => $id = Auth::id(),
我知道这应该是一个字符串,但为了向你解释我正在尝试做什么,我仍然没有找到任何解决方案。
这样做。
public function store(Request $request)
{
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$request->validate([
'content' => 'required',
'topic' => 'required',
'hashtag' => 'required'
]);
$data = $request->all();
$data['id'] = Auth::id();
$check = Tweets::create($data);
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}
删除此
'id' => $id = Auth::id(),
并添加
$data['id'] = Auth::id();
之前
$check = Tweets::create($data);
应该可以