【问题标题】:General error: 1364 Field 'uuid' doesn't have a default value一般错误:1364 字段 'uuid' 没有默认值
【发布时间】:2021-10-13 18:42:15
【问题描述】:

我已经尝试了几乎所有我知道的补救措施,但它不起作用。但是,当我使用命令 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中,你的工厂就可以工作了。
  • 感谢您与我分享。现在它工作正常,没有问题

标签: php laravel eloquent uuid


【解决方案1】:

在这种情况下,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()

【讨论】:

  • 我尝试了你的方法,它应该可以正常工作。非常感谢!!!
猜你喜欢
  • 2019-11-23
  • 2021-02-28
  • 2017-03-02
  • 2020-02-03
  • 2020-02-27
  • 2020-07-23
  • 2016-06-13
  • 2021-01-18
  • 2021-07-30
相关资源
最近更新 更多