0

We are customizing our key to use a unique identifier in our routing.

So this

http://127.0.0.1:8000/TEAMS/1

ends up like this

http://127.0.0.1:8000/TEAMS/91e11f44-2d89-4b5e-83e0-5cb92d0c0ebd

This works great but unfortunately we're getting an error at another point. When we create a new team, we can't access the id of that newly created team:

dd

You see that the primaryKey points at the id-attribute but this is missing in the attributes-array. So we can't access

$newTeam->id 
// or
$newTeam['id']

Currently we are fetching the team after creation with

$team = Team::where('identifier', $newTeam->identifier)->first();

but that seems to be redundant.

Does someone know how to resolve this? Thanks in advance.

Edit

This is the relevant part from the model

class Team extends JetstreamTeam
{
    use \App\Http\Traits\UsesIdentifier;

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


    /**
     * Specify the routing key
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'identifier';
    }
}

Migration:

class CreateTeamsTable extends Migration
{
    public function up()
    {
        Schema::create('teams', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->index();
            $table->string('name');
            $table->boolean('personal_team');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('teams');
    }
}

Trait:

trait UsesIdentifier
{
    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->identifier = (string) Str::uuid();
        });
    }

    public function getIncrementing()
    {
        return false;
    }

    public function getKeyType()
    {
        return 'string';
    }
}
Matthias
  • 3,729
  • 22
  • 37

1 Answers1

1

As there are insufficient information about your table's primary key, I can assume the following.

  • You have mentioned that you are using custom primary key. the identifier column. So, you don't have the id column in your table. The default find() method works on the id field.

  • To make it work with your customized identifier column, you have to define the primary key as below.

class User extends Model
{
    protected $primaryKey = 'identifier';
}

After this, you can access model like this:

$team = Team::find($newTeam->identifier);
S K R
  • 552
  • 1
  • 3
  • 16
  • Thanks for your response. I edit my question. We are still using an id as primary key to identifiy our entries. The identifier is just an add on to use for the routing. – Matthias Mar 04 '21 at 08:31
  • what is this used for ```\App\Http\Traits\UsesIdentifier``` ? – S K R Mar 04 '21 at 08:33
  • i think the ```id``` is added to hidden attribute somewhere inside the base class or the trait. – S K R Mar 04 '21 at 08:36
  • Oh sorry I forgot to mention, please see my edit. – Matthias Mar 04 '21 at 08:36
  • please remove this ```public function getIncrementing() { return false; }``` from the trait of its not necessary – S K R Mar 04 '21 at 08:39
  • Please remove ```getIncrementing()``` and ```getKeyBy()``` methods. and if necessary, use this ```$incrementning = false``` and ```$keyBy = 'string'``` as in https://laravel.com/docs/8.x/eloquent#primary-keys But I don't think that will be necessary as you are internally using ```id``` as primary key – S K R Mar 04 '21 at 08:43
  • id should not be in the fillable. it will work fine even when not in fillable @QamarRafhan – S K R Mar 04 '21 at 08:46
  • 1
    I removed the 'getIncrementing'-function and it seems to work. Thanks a lot!! Can you explain why 'getIncrementing' makes this problem? I don't understand.. – Matthias Mar 04 '21 at 08:48
  • in getIncrenmenting() you are returning false. so, it will not create id automatically by auto incrementting. so, causing the issue. – S K R Mar 04 '21 at 08:50