Laravel Eloquent Serialization

I have a table where the Primary Key looks like this: 123456-789 in the id column. When I query all the records from my table using $allArray = $this->all()->toArray() , I have a problem. All data goes as expected, but the id now looks like this: 123456789 . The id column is set as varchar(24) utf8_general_ci in the database.

When I print_r() my result from $all = $this->all() (without ->toArray() ), I see that the id was currently selected 123456-789 . Then I try to get id again, like echo $all[1]->id , this is 123456789 .

Any help would be greatly appreciated. Thanks:)

+5
source share
1 answer

You must let your model know that the primary key will not automatically increment the value, otherwise it will try to convert the primary key to an integer.

Just add this to your model.

 public $incrementing = false; 
+4
source

Source: https://habr.com/ru/post/1275563/


All Articles