Eloquent relations not working correctly #1362

Closed
JackPoint opened this Issue May 21, 2013 · 16 comments

Comments

Projects
None yet
6 participants

Is there something changed in the eloquent relationships?

The docs at http://four.laravel.com/docs/eloquent#querying-relations say I could replace

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

with

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 http://stackoverflow.com/questions/16656805/laravel-4-eloquent-one-to-many-relations-error

Contributor

brunogaspar commented May 21, 2013

Maybe you are forgot to use Eager Loading?

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

Contributor

bencorlett commented May 21, 2013

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.

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.

Owner

taylorotwell commented May 22, 2013

Paste your model class code.

Homephoto.php

<?php

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

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

        public $timestamps = false;

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

Photo.php

<?php

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');
    }

}

Shoot.php

<?php

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

<script>

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

</script>
Contributor

bencorlett commented May 22, 2013

That's because he's made the same mistake....

Sent from my iPhone
Please excuse my brevity

On 22/05/2013, at 10:06 PM, Jack Hage notifications@github.com wrote:

Perhaps http://stackoverflow.com/questions/16691006/laravel-4-hasmany-does-not-work has the same problem


Reply to this email directly or view it on GitHub.

@ghost

ghost commented May 23, 2013

@JackPoint is the following giving you the error?

$photo->photo->shoot->url

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

$photo->shoot->url

@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.

Contributor

bencorlett commented May 23, 2013

Unless I'm not following, you're trying to access eager loaded properties without actually using eager loading?

Sent from my iPhone
Please excuse my brevity

On 23/05/2013, at 6:57 PM, Jack Hage notifications@github.com wrote:

@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.


Reply to this email directly or view it on GitHub.

@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.

Contributor

franzliedke commented May 23, 2013

Yes, that is called lazy loading.

On Thu, May 23, 2013 at 11:23 AM, Jack Hage notifications@github.comwrote:

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


Reply to this email directly or view it on GitHubhttps://github.com/laravel/framework/issues/1362#issuecomment-18332317
.

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

@JackPoint JackPoint closed this May 23, 2013

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

Contributor

bencorlett commented Sep 14, 2013

I think that should be asked in the forums, irc, or by reading the docs.

Sent from my iPhone
Please excuse my brevity

On 15 Sep 2013, at 6:40 am, Franz Liedke notifications@github.com wrote:

http://laravel.com/docs/eloquent#relationships


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment