2

I am trying to push $user_id into an array and store it in a database.

But when I dd($children) I get an integer value, not an array. What am I doing wrong?

if($user->hasRole(['guardian','learner'])){
    $user_id = $user->id;
    $db =  Auth::user()->related;
    $children = array_push($db, $user_id);
    dd($children);

    return redirect()
        ->route('parent-child.index')
        ->with('message', $user->name . ' has been added Successfully');
}
2
  • 1
    define children as an array above your code and check again $children = []; Commented Nov 2, 2020 at 9:05
  • 1
    and this line is wrong : $children = array_push($db, $user_id); change it to: array_push($children , $user_id); Commented Nov 2, 2020 at 9:07

1 Answer 1

1

Try the following code:

if($user->hasRole(['guardian','learner'])){
    $db = []; //declare $db as an array
    $user_id = $user->id;
    $db =  Auth::user()->related;
    array_push($db, $user_id);
    $children = $db;
    dd($children);

    return redirect()
        ->route('parent-child.index')
        ->with('message', $user->name . ' has been added Successfully');
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.