Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 6如何存储已登录的用户&x27;控制器中的s id_Laravel_Laravel 6_Laravel Authentication_Laravel 6.2_Laravel Controller - Fatal编程技术网

Laravel 6如何存储已登录的用户&x27;控制器中的s 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.')

我正在尝试存储已登录用户的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);

这应该行得通

使用
Auth::id()
而不是
$id=Auth::id()
@Shahrukh我试过了,我也遇到了同样的问题