Accessing nested relationship with Laravel 4

若如初见. 提交于 2019-12-23 15:24:50

问题


I'm having trouble figuring out how to access a nested relationship within Laravel. The specific example I have is a Movie that has many entires in my Cast table which has one entry in my People table. These are my models:

MOVIE

class Movie extends Eloquent {

    protected $primaryKey = 'movie_id';
    protected $table = 'movie';

    // Relationships
    public function cast()
    {
        return $this->hasMany('MovieCast', 'movie_id');
    }
}

MOVIECAST

class MovieCast extends Eloquent {

    protected $table = 'movie_cast';

    public function person()
    {
        return $this->hasOne('Person', 'person_id');
    }

    public function netflix()
    {
        return $this->belongsTo('Movie', 'movie_id');
    }
}

PERSON

class Person extends Eloquent {

    protected $primaryKey = 'person_id';
    protected $table = 'people';

    public function movieCast()
    {
        return $this->belongsTo('MovieCast', 'person_id');
    }
}

In my controller I can access the cast (containing person_id and role_id) like so:

public function movie($id)
    {
        $movie = Movie::find($id);
        $cast = $movie->cast();

        return View::make('movie')->with(array(
            'movie' => $movie,
            'cast' => $cast
        ));
    }

...but I don't know how to access the corresponding name field in my People table.

EDIT 1:

Using the classes exactly as defined below in @msturdy's answer, with the controller method above I try to render the person names like so inside my view:

@foreach($cast->person as $cast_member)
        {{$cast_member->person->name}}
    @endforeach

Doing this i get the error:

Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$person

I don't know if it makes a difference or not but I have no id field on my People table. person_id is the primary key.


回答1:


It should be simple, once you have accessed the cast...

Route::get('movies', function()
{
    $movie = Movie::find(1);
    $cast  = $movie->cast;

    return View::make('movies')->with(array(
        'movie' => $movie,
         'cast' => $cast));
});

Note: at this point, $cast is an instance of the Collection class, not a single object of the MovieCast class, as the relationship is defined with hasMany()

you can iterate over it in the View (in my case /app/views/movies.blade.php

  @foreach($cast as $cast_member)
    <p>{{ $cast_member->person->name }}</p>
  @endforeach

Class definitions used for testing:

class Movie extends Eloquent {

    protected $primaryKey = 'movie_id';
    protected $table = 'movie';
    public $timestamps = false;

    // Relationships
    public function cast()
    {
        return $this->hasMany('MovieCast', 'movie_id');
    }
}

class MovieCast extends Eloquent {

    protected $table = 'movie_cast';
    public $timestamps = false;

    public function person()
    {
        return $this->hasOne('Person', 'person_id');
    }

}

class Person extends Eloquent {

    protected $primaryKey = 'person_id';
    protected $table = 'people';
    public $timestamps = false;

    public function movieCast()
    {
        return $this->belongsTo('MovieCast', 'person_id');
    }
}


来源:https://stackoverflow.com/questions/20316845/accessing-nested-relationship-with-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!