0

I am trying to store logged user's id but I am getting this error

ErrorException
array_map(): Argument #2 should be an array

This is the code in the controller

    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.');
    }

The error is in this line of code.

'id' => $id  = Auth::id(),

I know that should be a string but to explain to you what I am trying to do, and I still have not found any solution.

Kostandin Vllahu
  • 231
  • 7
  • 16

2 Answers2

1

Do it Like this.

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.');
}
Shahrukh
  • 399
  • 2
  • 15
1

Delete this

'id' => $id  = Auth::id(),

and add

$data['id'] = Auth::id();

before

$check = Tweets::create($data);

That should work