I have 3 tables:
categories
admins
users
Category can create users and admins. In categories
table I have morphs
field:
$table->nullableMorphs('userable');
And relations in Category
model:
public function userable()
{
return $this->morphTo();
}
public function user()
{
return $this->morphOne(self::class, 'userable');
}
But when I tried do like this:
$category = Category::first();
$user = User::first();
$category->user()->save($user);
Get error with message:
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'userable_id' in 'field list' (SQL: update
users
setuserable_id
= 1,userable_type
= App\Models\Category,users
.updated_at
= 2021-04-27 12:13:22 whereid
= 1)'
How I can correctly create self morph relationship in laravel?