0

I want save user id comes with create tag in post create form , but show me this error

key() expects parameter 1 to be array, integer given

post controller:

public function store(PoemsCreateRequest $request)
{
    $data = $request->all();
    $post = Post::create($data);
    if ($post && $post instanceof Post) {
        $tagList = $request->input('tags');
        $tags = collect($tagList)->mapToGroups(function (string $tag) {
            return Tag::firstOrCreate(['name' => $tag, 'author' => Auth::id()])->id;
        })->all();
        return redirect()->back();
    }

}

how to fix it?

1
  • Can we see a full stack trace on what line of yours it fails? Commented Sep 11, 2020 at 21:47

1 Answer 1

1

The closure that you passed into the mapToGroups() function is returning an int, which is not correct. It must return an array.

From the documentation:

The mapToGroups method groups the collection's items by the given callback. The callback should return an associative array containing a single key / value pair, thus forming a new collection of grouped values

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.