Skip to content
Advertisement

Laravel | Why is the id missing in a eloquent model?

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

So this

JavaScript

ends up like this

JavaScript

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

JavaScript

Currently we are fetching the team after creation with

JavaScript

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

JavaScript

Migration:

JavaScript

Trait:

JavaScript

Advertisement

Answer

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.

JavaScript

After this, you can access model like this:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement