0
votes

Im am using laravel 7. I have 2 tables, products and testimonials. Each testimonial is related to a product. So i made an 2 relationships:

  • Product: hasMany('App\Models\OM\Testimonial');
  • Testimonial: belongsTo('App\Models\OM\Product', 'product_id')

But when i dd(Testimonial->with('product)) i get this

array:1 [▼ "testimonials" => Illuminate\Database\Eloquent\Builder {#347 ▼ #query: Illuminate\Database\Query\Builder {#358 ▶} #model: App\Models\OM\Testimonial {#359 ▼ #table: "om_testimonials" #fillable: array:4 [▶] #connection: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: false +wasRecentlyCreated: false #attributes: [] #original: [] #changes: [] #casts: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #guarded: array:1 [▶] } #eagerLoad: array:1 [▶] #localMacros: [] #onDelete: null #passthru: array:19 [▶] #scopes: [] #removedScopes: [] } ]

1
Testimonial::with('product')->get(); this would workAkhtar Munir

1 Answers

1
votes

It's normal, with asks Eloquent for eager-loading your relationship but it does not retrieve it yet because it allows you to add constraints on your "query". You need to do this to retrieve your models and their relationship

Testimonial::with('product')->get();

You should also check the documentation, every detail is here: https://laravel.com/docs/7.x/eloquent-relationships#eager-loading