Get ID from new record in Laravel

时间:2019-04-16 22:05:50

标签: laravel-5 eloquent laravel-5.8

I am creating a new user. After creating, I need the ID of the user, which is automatically set by the database.

$user = new User();
$user->save();
dd($user->id);

But the response I get is null. Is there a way to get the ID of the user?

I use Sentinel to manage my users. I have adapted the users migration to my needs. Now it looks like this:

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('first_name');
    $table->string('last_name');
    $table->string('username')->nullable()->unique();
    $table->string('email')->nullable();
    $table->string('password')->nullable();
    $table->text('permissions')->nullable();
    $table->string('doodle_token', 64)->nullable()->unique();
    $table->timestamps();

    $table->engine = 'InnoDB';
});

I'm using Laravel version 5.8

0 个答案:

没有答案