Skip to content
Advertisement

Laravel: Why are results from the database being displayed in the view differently than what Dump&Die says is retrieved?

I have a table called parts. The primary key is part_id, it is not to be incremented, it is a VARCHAR (Laravel string). All part id’s are 2 numbers, hyphen, 4 numbers (e.g. 12-3456)

When the results from a query are displayed in the web browser they are not entirely the same as what Dump&Die <?php dd($parts); ?> says is being retrieved…

Here is what I have figured out from adding test parts to the database:

  • It seems zeros before a single digit are not displayed. (e.g. 04 is displayed as 4, 44 is displayed as 44)
  • The character - is not displayed and nor are any other characters that follow this. (e.g. 04-0001 is displayed as 4, 44-4444 is displayed as 44)
  • An entry with just letters displays as a 0. (e.g. hello is displayed as 0, g is displayed as 0)
  • A string of only numbers is displayed as is expected. (e.g. 1234 is displayed as 1234, 123456789 is displayed as 123456789)

Any ideas why this would be? I’m completely stumped…

Code and screenshots below for clarity.

Screenshot from browser with dd() function called.

This screenshot shows the result displayed in the browser 1 and the result from the dd() function 01-2222.

The Laravel view in question:

JavaScript

My migration for this table:

JavaScript

The Controller:

JavaScript

The Model:

JavaScript

If any more information is needed to help debug this please ask me to provide it. I’m sure I’ve missed something silly but I keep looking over the code and can’t see it…

Advertisement

Answer

Because you are using the part_id as your primary key. This is a poor choice, and unneccessary for the sake of one column on the database.

You need to set public $incrementing=false; on the model to tell Eloquent not to cast the column as an unsigned integer.

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