A quick query regarding some data model relations in Laravel.
I have a product, that belongs to a user, but a user can have many products. But a product can also have many people making an offer on that product so;
class Product extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function offer(){
return $this->hasMany('App\Offer');
}
But a product can also have different sub types of product, for example, one might be a vehicle, or a mobile phone, each with different columns in my migration, so I assume I need morph many
In Product.php
public function imageable()
{
return $this->morphTo();
}
In Vehicle.php
public function products()
{
return $this->morphMany('App\Product', 'imageable');
}
However, when calling:
$product = Product::findOrFail($id);
I get null when I attempt do anything like $product->vehicle
I add the ID of the vehicle in my database to the imageable_id
on the products table, and the imageable
type to 'vehicle' on the products table too.
What am I doing wrong here?
'Class 'vehicle' not found'
– Lang