第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Laravel model::create() 不返回主鍵

Laravel model::create() 不返回主鍵

PHP
夢(mèng)里花落0921 2023-06-24 15:09:42
我有一個(gè)以下身份驗(yàn)證表:<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Schema;class CreateAuthTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('auth', function (Blueprint $table) {            $table->uuid('id')->primary();            $table->string('email', 255)->index()->unique();            $table->string('password', 96);            $table->timestamps();        });        DB::statement('ALTER TABLE auth ALTER COLUMN id SET DEFAULT uuid_generate_v4();');    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('auth');    }}這是模型:<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Model;use Illuminate\Foundation\Auth\User as Authenticable;use Illuminate\Support\Facades\Hash;use Laravel\Passport\HasApiTokens;class User extends Authenticable{    use HasApiTokens;    protected $table = "auth";    protected $fillable = ["email", "password"];    public $incrementing = false;    protected $keyType = 'string';    protected $casts = [        'id' => 'string'    ];    private $hashOptions = [        'memory' => 1024,        'time' => 2,        'threads' => 1    ];    public function setPasswordAttribute($value)    {        $this->attributes['password'] = Hash::make($value, $this->hashOptions);    }}
查看完整描述

1 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

只需確保強(qiáng)制轉(zhuǎn)換為字符串且主鍵名稱正確


namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model

{

  protected $casts = [

    'id' => 'string'

  ];

  protected $primaryKey = "id";

}

如果你將 $incrementing 設(shè)置為 false,它會(huì)破壞它并始終返回 false,因?yàn)樗嬖V代碼該 id 不是自動(dòng)生成的,因此永遠(yuǎn)不會(huì)被獲取。


class User extends Model

{

  public $incrementing = false;

  // ...

}


$user = new User;

$user->save();

dd($user->id);

// null

還要確保在遷移中啟用 uuid 擴(kuò)展


<?php


class AddUuidExtensionToPostgre extends Migration

{


    public function up()

    {

        DB::statement('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";');

    }


    public function down()

    {

        DB::statement('DROP EXTENSION IF EXISTS "uuid-ossp";');

    }

}

這是一個(gè)例子



Schema::create('users', function (Blueprint $table) {

    $table->uuid('id');

    $table->primary('id');

    // ...

});


DB::statement('ALTER TABLE users ALTER COLUMN id SET DEFAULT uuid_generate_v4();');


查看完整回答
反對(duì) 回復(fù) 2023-06-24
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)