0
votes

I got this error

"Method Illuminate\Database\Eloquent\Collection::save does not exist."

when i'm trying to Register User API.

i dont know what 's wrong with it...

My AuthController

    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required|min:3',
            'email' => 'required|email',
            'password' => 'required|between:6,25',
        ]); 

        $user = User::all();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();

        return response()->json([
            'registered' => true,
            'token' => $user->createToken('token')->accessToken
        ]);
    }

Any help? Thanks....

2
Laravel comes with built-in authentication - suggest you use that instead. That said, you don't want $user = User::all(), as that gets a collection of all users, instead you want $user = new User;Qirel
it's my stupid mistake, thanks.... @Qirelg3tl0st

2 Answers

2
votes

$user = User::all(); return a collection of all users in database, you should replace it with $user = new User;, which return a new User object.

0
votes

Change code to

public function register(Request $request)
{
    $request->validate([
        'name' => 'required|min:3',
        'email' => 'required|email',
        'password' => 'required|between:6,25',
    ]); 

    $user = new User();
    $user->name = $request->name;
    $user->email = $request->email;
    $user->password = bcrypt($request->password);
    $user->save();

    return response()->json([
        'registered' => true,
        'token' => $user->createToken('token')->accessToken
    ]);
}