简体   繁体   中英

Getting NULL on BelongsTo Relationship in Laravel

在此处输入图片说明

First of all, I'm a beginner to Laravel. So in the image above if I do a dump of the car variable that takes data from both the Car model and Gallery model based on an id, it returns all of that. However, if I say $car->gallery_photo, which is in the Gallery model, it returns null.

I have already put the foreign key in both of the models [belongsTo() and hasMany()]. Why am I not accessing the relations object? What am I doing wrong?

PS I'm typing from my phone, so at time of posting I could not show code snippets.

Ok so I solved the issue. To access the results from Gallery model I looped through $car->gallery. It was that simple lol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM