Reputation: 401
I am trying to get ralation (product owner) using with
method but it returns me null
.
After some tests i see my relation is ok if im using simple way to get it ($goodRelation
in example), but if im trying to get relation with with
method it any way returns null
EXAMPLES
$goodWith = Good::where('id', '4d7d36f2-75ad-4a9f-a5a5-ba8b56840414')->with('user')->first()->user;
$goodsLoad = Good::where('id', '4d7d36f2-75ad-4a9f-a5a5-ba8b56840414')->first()->load('user')->user;
$goodFirstUserFirst = Good::where('id', '4d7d36f2-75ad-4a9f-a5a5-ba8b56840414')->first()->user()->first();
$goodsRelation = Good::where('id', '4d7d36f2-75ad-4a9f-a5a5-ba8b56840414')->first()->user;
dd($goodWith, $goodsLoad, $goodFirstUserFirst, $goodsRelation);
Result:
$goodWith -> NULL
$goodsLoad -> NULL
$goodFirstUserFirst -> USER MODEL
$goodsRelation -> USER MODEL
My User
model:
public function goods()
{
return $this->hasMany(Good::class);
}
My Good
model:
public function user()
{
return $this->belongsTo(User::class);
}
Why i can not get relation with with
of load
method ?
Upvotes: 2
Views: 998
Reputation: 4110
From the Laravel 7 docs;
If your primary key is not an integer, you should set the protected $keyType property on your model to string:
<?php
class Flight extends Model
{
/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';
}
Upvotes: 1
Reputation: 401
SOLUTION: If you are using uuid
or not default integer
- you should set $keyType
in your model.
class Flight extends Model
{
/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';
}
Upvotes: 0