1

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
  • 1
    Testimonial::with('product')->get(); this would work Commented May 12, 2020 at 7:36

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Well that's so stupid of me :))). I was missing de ->get(). Thank you!
0

The issue is that Testimonial::with('product') only prepares the query but does not execute it.
You need to actually retrieve the data.

Use the get() method to execute the query:

$testimonials = Testimonial::with('product')->get();
dd($testimonials);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.