Search code examples
phplaraveluuid

Unknown column 'uuid' in 'field list' in laravel


i am trying to create a column "unique" filled with UUID in my "users" table i don't want it to be primary and as i try to register user i am facing this error "Unknown column 'uuid' in 'field list'" i have made a new laravel project and just used "Auth" command on it If You Have Any Other Way To Execute This Please Do Share i just want a unique 5 to 8 character code for my user

Here's My Users Migration:-

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->uuid('unique');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

Here's My User Model:-

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Webpatser\Uuid\Uuid;
use App\Models\Concerns\UsesUuid;
use Illuminate\Support\Str;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

     protected $guarded = []; // YOLO



  public $incrementing = false;


   protected $keyType = 'string';

   protected static function boot()
   {
     parent::boot();
  self::creating(function ($user) {
      $user->uuid = (string) Uuid::generate(4);
  });
   }


    protected $fillable = [
        'name', 'email', 'password', 'uuid',
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


}

Here's a Picture Of My DB:- enter image description here

this is my first time using UUID so i did as per the tutorials as i am a beginner at laravel as well as programming i don't know whats casing the problem Please Help

-ThankYou


Solution

  • Your migration script creates a column unique not uuid, that's the reason you are getting column uuid not not found. (please look at to your phpmyadmin screenshot second column)

    change this $table->uuid('unique'); to $table->uuid('uuid')->unique(); that may create you a column for uuid.