Reputation: 23
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:
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: [] } ]
Upvotes: 1
Views: 1665
Reputation: 9
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);
Upvotes: 0
Reputation: 3529
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
Upvotes: 3