Laravel 6如何存储已登录的用户&x27;控制器中的s id
我正在尝试存储已登录用户的id,但出现此错误Laravel 6如何存储已登录的用户&x27;控制器中的s id,laravel,laravel-6,laravel-authentication,laravel-6.2,laravel-controller,Laravel,Laravel 6,Laravel Authentication,Laravel 6.2,Laravel Controller,我正在尝试存储已登录用户的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.')
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);
这应该行得通使用
Auth::id()
而不是$id=Auth::id()
@Shahrukh我试过了,我也遇到了同样的问题