try
$feeds = \App\Activityfeed::with(['feedable.product', 'feedable.business'])->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi all,
I have two activities (ratings, events) that go into a feed table through a polymorphic relationship. When I do
$feeds = \App\Activityfeed::with('feedable')->get();
It works fine, giving me 2 different types of activities and that data. However I want to pull in the related product data for my rating activity and the business data for the event activity. So I figured
$feeds = \App\Activityfeed::with('feedable.product', 'feedable.business')->get();
However this gives me an error.
Call to undefined relationship [product] on model [App\Event]
My model structure is below. What am i missing?
Modals: Businesses Modal
class Business extends Model
{
use Taggable;
// Fillable fields for a business
protected $fillable = [
'name',
'street',
'city',
'state',
'zip',
'country',
];
// A Business is owned by a user
public function owner() {
return $this->belongsTo('App\User', 'user_id' );
}
// A business is composed of many products
public function products()
{
return $this->hasMany(Product::class);
}
// A business can have many events
public function events()
{
return $this->hasMany(Event::class);
}
}
Events
class Event extends Model
{
// Fillable fields for a event
protected $fillable = [
'name',
'day',
'start',
'end',
'price',
'description',
'image',
];
// An event was created by a user
public function user()
{
return $this->belongsTo(User::class);
}
// An event belongs to a business
public function createdBy()
{
return $this->belongsTo('App\Business');
}
// Get all of ratings activities
public function activityfeeds()
{
return $this->morphMany('App\Activityfeed', 'feedable');
}
}
Products Modal
class Product extends Model
{
// Fillable fields for a product
protected $fillable = [
'name',
'type',
'description',
'img_url',
];
// A product belongs to a business
public function creator()
{
return $this->belongsTo('App\Business');
}
// Product has many ratings
public function ratings()
{
return $this->hasMany(Rating::class);
}
}
Ratings Modal
class Rating extends Model
{
// Rating belongs to a user
public function user()
{
return $this->belongsTo(User::class);
}
// Rating belongs to a product
public function product()
{
return $this->belongsTo(Product::class);
}
public function activityfeeds()
{
return $this->morphMany('App\Activityfeed', 'feedable');
}
}
Activityfeed
class Activityfeed extends Model
{
protected $fillable = ['user_id'];
/**
* Get all of the owning feedable models.
*/
public function feedable()
{
return $this->morphTo();
}
}
Controller
$feeds = \App\Activityfeed::with('feedable.product', 'feedable.business')->get();
dd($feeds->toArray());
Please or to participate in this conversation.