General error: 1364 Field 'uuid' doesn't have a default value

General error: 1364 Field 'uuid' doesn't have a default value

我几乎尝试了所有我知道的补救措施,但都没有用。但是,当我使用命令 php artisan db:seed seed 时,它会正常工作

一般错误:1364 字段 'uuid' 没有默认值 enter image description here

这是我的模特:

<?php

namespace App\Models;

use App\Enums\ProjectStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'status',
        'user_id',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'id',
        'uuid',
    ];

    protected $cast = [
        'status' => ProjectStatus::class
    ];


    public function getRouteKeyName()
    {
        return 'uuid';
    }
}

这是我的工厂:

<?php

namespace Database\Factories;

use App\Enums\ProjectStatus;
use App\Models\Project;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
// use Illuminate\Support\Str;

class ProjectFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Project::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $status = ProjectStatus::getRandomValue();
        //in function
        // $uuid = Str::uuid()->toString();

        return [
            'uuid' => $this->faker->uuid(),
            'user_id' => User::factory(),
            'name' => $this->faker->word(),
            'status' => $status,
        ];
    }
}

这是我的迁移:

<?php

use App\Enums\ProjectStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProjectsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->index();
            $table->string('name');
            $table->string('status')->default(ProjectStatus::Pending);
            $table->foreignId('user_id')->constrained('users');
            $table->timestamps();
        });
    }

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

这是我的控制器:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Resources\ProjectResource;
use App\Models\Project;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $projects = Project::all();

        return response([
            'products' => ProjectResource::collection($projects),
            'message' => 'Retrieve products successfully'
        ], 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        return Project::create($request->all());
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

在这种情况下,uuid 必须是 fillable。所以只需将 uuid 添加到 fillable。我还希望在创建新对象时由模型自动设置 uuid

所以在你的模型中添加一个启动函数,用于在创建对象时设置一个 uuid。

use Illuminate\Support\Str;

public static function boot()
{
    parent::boot();
    
    static::creating(function ($model) {
        $model->uuid = Str::uuid();
    });
}

根据 Laravel 版本,您可以创建一个 uuid,例如通过 Str::uuid().