When I make a new table in Laravel, I generally use the Laravel default id column definition: $table->bigIncrements('id');
Say for example I have a clients
table with that in my migration.
If I need a foreign relationship on another table, I 'll do it like this:
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
However, if I get model instances of both of those tables and render them with ->toJson();
, the id
of the first table will be an integer in JSON, while the client_id
of the second table will be a string in the json output.
Now I can make a $casts=[]
array in the Model class for the second table to cast it to an integer, but I don't know if there's any reason I shouldn't do that. Or maybe I should ALWAYS do that. Is there any guidelines or suggestions about this sort of thing?