Skip to content
This repository

Eloquent relations not working correctly #1362

JackPoint opened this Issue May 21, 2013 · 17 comments

6 participants

Jack Hage Bruno Gaspar Ben Corlett Taylor Otwell Franz Liedke Anouar Absslm
Jack Hage

Is there something changed in the eloquent relationships?

The docs at say I could replace

echo $phone->user()->first()->email;


echo $phone->user->email;

But I get errors of undefined properties when I do that. It worked before but after an update not. I think its a change between now and 2 weeks ago.

Sometimes it works when I do somethink like

echo $phone->user()->get();

But I didn't take the time to exactly figure out whats wrong. What can I do to reproduce/find the error?

See also the issue on

Bruno Gaspar

Maybe you are forgot to use Eager Loading?

Jack Hage

That doesn't work. And I don't think thats the problem, they must autoload when you need them.

I get this error: Trying to get property of non-object

Ben Corlett

Dude, try eager loading. If $phone->user returns null, of course you're going to get that error when you try access the email property of user.

Jack Hage

I tryed, but that doesn't resolve my problem.

From the docs:

Eloquent will automatically load the relationship for you

Eager loading exists to alleviate the N + 1 query problem.

So it's not necessary to use eager loading.

Taylor Otwell

Paste your model class code.

Jack Hage



class Homephoto extends Eloquent {
        protected $table = 'homephoto';

        protected $fillable = array('order_id', 'photo_id');

        public $timestamps = false;

        public function photo(){
                return $this->belongsTo('Photo');



class Photo extends Eloquent {
    protected $table = 'photos';
    public $timestamps = false;

    protected $fillable = array('src', 'shoot_id', 'in_shoot', 'order_id', 'direction');

    public function shoot(){
        return $this->belongsTo('Shoot');




class Shoot extends Eloquent{

    protected $table = 'shoots';

    protected $fillable = array('name', 'url', 'category_id', 'description', 'is_visible', 'taken_at', 'photo_id');

    public function photo(){
        return $this->belongsTo('Photo');

    public function photos(){
        return $this->hasMany('Photo')->orderBy('order_id');

    public function category(){
        return $this->belongsTo('Category');

In my controller i do this

    $photos = Homephoto::orderBy('order_id')->get();

    return View::make('home')
                 ->with('photos', $photos);

And in my view


    @foreach($photos as $photo)
      '{{ URL::to('/image/home/'.$photo->photo_id.'-'.$photo->photo->shoot->url.'.jpg') }}',
    {duration: 3000, fade: 1000});

Ben Corlett
Deleted user
ghost commented May 22, 2013

The content you are editing has changed. Reload the page and try again.

@JackPoint is the following giving you the error?


If so, you do not have a property/function in your Photo class called photo. I think the correct code would be

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Jack Hage

@bencorlett what mistake? Do you mean eager loading?

@gilryn the $photo you see is from the Homephoto model, not the Photo model. The Homephoto belongsTo the Photo model.

Ben Corlett
Jack Hage

@bencorlett thats right, I try to access a property without using eager loading. I quoted the docs in this comment #1362 (comment) They tell me the relationship is loaded automatically.

Franz Liedke
Jack Hage

I updated to the latest version and it seems the error is gone. Strange.

Jack Hage JackPoint closed this May 23, 2013
Anouar Absslm

Hi guys ,
I'm trying to convert L3 bundle to L4 package and the old bundle use has_many_and_belongs_to . any idea of the new use of this in L4

Here is my code :

 public function groups() {
   return $this->has_many_and_belongs_to('Group', 'user_groups');

Thanks for advance

Ben Corlett
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.