Following up on my last night's question, I had a good night of sleep and "discovered" that this happens due to automatic casting / type conversion or whatever.
To summerize:
uuid
as primary keytrigger
on insert from mySQL$model_object->fresh();
fresh()
Model
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Address extends Model {
protected $table = 'Addresses';
protected $fillable = ['uuid', 'zipCode', 'houseNumber'];
protected $primaryKey = 'uuid';
//public $incrementing = false;
}
Controller (where it screws up)
public function store(Request $request) {
$input = $request->all();
$address = Address::create($input);
var_dump($address); exit;
//result (as expected)
$address = $address->fresh();
var_dump($address); exit;
//result (as expected)
var_dump($address->uuid); exit;//result (wow): int(0)
}
UUIDs are created by your application, so you don't have to wait for the database server to create new users. Since you create your UUIDs independent of the database, you can split your data between multiple database servers. UUIDs are secure, as they're randomly generated and unique, so they can't be guessed.
Casting a value means changing it to (or ensuring it is already) a particular type. Some types you might be familiar with are Integer or Boolean . Simply put, type casting is a method of changing an entity from one data type to another.
For those coming here later. You can override the $keyType
variable in your model with the string type:
protected $keyType = 'string';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With