Home > Net >  Cannot use save method when updating user info in laravel
Cannot use save method when updating user info in laravel

Time:05-11

So I'm trying to create a multi-page user registration. I have the first page working and I can add user data to my database. After I add this I log the user in. However, when I go to the second page, I use

$user = Auth::user();

to get the logged in user. And then when I go to add the new info to the correct fields, I cannot use the

$user->save();

method because it says "undefined method 'save'".

I have no idea what is going wrong.

This is my RegisterController.php file:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RegisterController extends Controller
{
    public function sign_up() {
        return view('register.sign_up1');
    }

    public function sign_up2() {
        return view('register.sign_up2');
    }

    public function login() {
        return view('register.login');
    }

    public function sign1() { //Page 1 sign up
        $attributes = request()->validate([
            'name' => ['required'],
            'email' => ['required'],
            'password' => ['required'],
            'co_password' => ['required']
        ]);
        $user = new User();
        $user->name = $attributes['name'];
        $user->email = $attributes['email'];
        $user->password = $attributes['password'];
        $user->co_password = $attributes['co_password'];
        $user->save();
        auth()->login($user);
        return redirect('sign_up2');
    }
    public function sign2(Request $request) { //Page 2 sign up
        $user = Auth::user();
        $request->validate([
            'add1' => ['required'],
            'state' => ['required'],
            'zip' => ['required']
        ]);
        // $user->name = $user->get('name');
        // $user->email = $user->get('email');
        // $user->password = $user->get('password');
        // $user->co_password = $user->get('co_password');
        $user->add1 = $request['add1'];
        $user->save();
        
        
        return redirect('/');
        
    }


}

This is my User model file:


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class User extends Model implements AuthenticatableContract
{
    use HasFactory;
    use Authenticatable;
}

and this is my migration for users:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->string('co_password'); //confirm password
            $table->string('add1')->nullable(); //address line 1
            $table->string('add2')->nullable(); //address line 2
            $table->string('state')->nullable(); //state
            $table->integer('zip')->nullable(); //zip code
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
};

Ive included the error message I get here Error Message

CodePudding user response:

Instead of this

$user = Auth::user();

Use this

$user_id = Auth::user()->id;
$user = User::where('id',$user_id )->first();
$user->add1 = $request['add1'];
$user->save();

CodePudding user response:

It is safe to ignore that error. Since Auth::user() is returning an instance of App\Models\User, it is perfectly safe. Only reason your IDE thinks it's wrong is because Auth::user() is supposed to return Authenticatable(Illuminate\Foundation\Auth\User). Since your User model implements Authenticatable, you can access model functions like save()

CodePudding user response:

Try this.

$user = User::find(Auth::user()->id);
$user->add1 = $request['add1'];
$user->name = $attributes['name'];
$user->email = $attributes['email'];
$user->password = $attributes['password'];
$user->co_password = $attributes['co_password'];
$user->save();
  • Related