If value of foreign key is null, then it dies with error ERROR: invalid input syntax for uuid: "0" on postgres database.
@mattstauffer Please look into it.
Needs more information on how to recreate.
It would die with pgsql driver.
...
// Schema teams table
Schema::create('teams', function (Blueprint $table) {
$table->uuid('id')->primary();
...
// Schema users table
Schema::create('users', function (Blueprint $table) {
$table->uuid('team_id');
...
class User extends Model {
...
public function team() {
return $this->belongsTo(Team::class);
}
...
$user->with(['team'])->all();
...
Any update on this? Having the same issue in 5.1. Can't eager load an association if the foreign key value is null (UUID column).
Created an identical PR against 5.1 https://github.com/laravel/framework/pull/12661
@GrahamCampbell any explanation to closing this?
Having the same issue. There are a couple of changes to BelongsTo that seem to work, but does need some tests.
Is there anyway to replace BelongsTo with a custom implementation?
Okay, I have a fix that's pretty easy to implement - https://gist.github.com/EspadaV8/85d4ea315c9834d58810
Put those files somewhere in your project and update all your UUID models to extend App\Database\Eloquent\Model instead of Illuminate\Database\Eloquent\Model and things should work without a problem.
All it does is replace 1 line in the BelongsTo class (https://gist.github.com/EspadaV8/85d4ea315c9834d58810#file-belongsto-php-L34) so that it returns an array with the NIL UUID (00000000-0000-0000-0000-000000000000) instead of the number 0. The Model then returns an instance of that class instead of the usual Laravel BelongsTo class.
It does assume you have the Rhumsaa\Uuid package installed already.
@rsahai91 Does your model have the public property $incrementing set to false ?
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.
Most helpful comment
@rsahai91 Does your model have the public property
$incrementingset tofalse?laravel doc