8.x Factory documentation refers to $user->type in the examples, but this is 'null' #36531
Unanswered
JonathanH-UK
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey folks,
I'm still finding my feet with Laravel but I've been trying to implement a polymorphic relationship and hit some problems trying to seed the database from simple factories, which I think are due to a small bug in the documentation.
In brief, I've made a table called 'products' including columns for 'sellable_id' and 'sellable_type'.
On two other models, I've added a product() function which returns
$this->morphOne(Product::class,'sellable')
as per the Eloquent document examples.In the second code snippet in the following section of the docs at https://laravel.com/docs/8.x/database-testing#defining-relationships-within-factories it demonstrates what I'm doing although I'm creating Products with two different related modules.
When I tried to use the
->type
property of my models it was Null and therefore triggering an SQL Exception.I've since played in artisan tinker and found that for all instances of User or my own models, calling $thing->type is always null.
The objective is to place the classname into the database column so I've got mine working by using
get_classname()
against the Model - e.g.From
return User::find($attributes['user_id'])->type;
To
return get_classname(User::find($attributes['user_id']));
I'm using Laravel 8 with JetStream and LiveWire installed.
Can anyone please confirm or deny whether they agree about the docs being wrong? I'm happy to raise a bug myself but would love a second opinion first.
Beta Was this translation helpful? Give feedback.
All reactions