与Laravel进行嵌套关系4 [英] Accessing nested relationship with Laravel 4

查看:200
本文介绍了与Laravel进行嵌套关系4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何访问Laravel内的嵌套关系。具体的例子是一个电影,在我的表中有许多内容,在我的人物表中有一个条目。这些是我的模型:



MOVIE

  class Movie扩展Eloquent {

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

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

MOVIECAST

  class MovieCast扩展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');
}
}

在我的控制器中,我可以访问cast(包含 person_id role_id )如下所示:

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

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

...但我不知道如何访问相应的 name 字段在我的 People 表中。



编辑1:



使用上面定义的类,在@ msturdy的答案中,使用上面的控制器方法,我尝试渲染 我的视图中的名称如下:

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

这样做我得到错误:



未定义的属性:Illuminate\Database\Eloquent\Relations\HasMany :: $ person





我不知道它是否有所作为,但我没有 id 字段在我的 People 表。 person_id 是主键。

解决方案

应该很简单,一次你已经访问了演员...

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

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




注意:此时, code> $ cast 是 Collection 类的一个实例,而不是 MovieCast class,因为
关系用 hasMany()


您可以在查看中迭代(在我的情况下 /app/views/movies.blade.php @endforeach






用于测试的类定义:

  class Movie extends雄辩{

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

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

class MovieCast扩展Eloquent {

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

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

}

class Person扩展Eloquent {

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

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


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.

解决方案

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

这篇关于与Laravel进行嵌套关系4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆