Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉威尔多对多:电影人物角色_Laravel_Eloquent - Fatal编程技术网

Laravel 拉威尔多对多:电影人物角色

Laravel 拉威尔多对多:电影人物角色,laravel,eloquent,Laravel,Eloquent,我希望有以下方面的模型和关系: 电影 人 角色(导演、剧本、演员等) 如果只是电影和人物,我可以设置一个(创建人物角色迁移并将$this->belongtomany()添加到人物和角色模型) 然而:人物X可以是电影A中的演员,也可以是电影B中的演员和导演 我看到了,但不确定这是如何在拉雷维尔做到的。(更新:Django的答案是“role”是角色的名字。在我的例子中:role=actor/director/writer/…) 更新:有关环境和我的尝试的更多详细信息 我创建了电影、人物、角色和查

我希望有以下方面的模型和关系:

  • 电影
  • 角色(导演、剧本、演员等)
如果只是电影和人物,我可以设置一个(创建
人物角色
迁移并将
$this->belongtomany()
添加到人物和角色模型)
然而:人物X可以是电影A中的演员,也可以是电影B中的演员和导演 我看到了,但不确定这是如何在拉雷维尔做到的。(更新:Django的答案是“role”是角色的名字。在我的例子中:role=actor/director/writer/…)

更新:有关环境和我的尝试的更多详细信息

我创建了电影、人物、角色和查找表movie_person_role(也称为三重透视):

方法:

class Movie extends Model
{
    public function persons() {
        return $this->belongsToMany('App\Person', 'movie_person_role', 'movie_id', 'person_id')->withPivot('role_id');
    }

    public function roles() {
        return $this->belongsToMany('App\Role', 'movie_person_role', 'movie_id', 'role_id')->withPivot('person_id');
    }
}
播种机:

        $id = DB::table('movies')->insertGetId([
            'name' => 'Redbad',
        ]);
        $m = App\Movies::find($id);
        $w->persons()->attach([
            App\Person::name('Roel Reiné')->first()->id => ['role_id' => App\Role::name('director')->first()->id],
            App\Person::name('Alex van Galen')->first()->id => ['role_id' => App\Role::name('writer')->first()->id],
            App\Person::name('Jonathan Banks')->first()->id => ['role_id' => App\Role::name('actor')->first()->id],
        ])

(可能有更清晰的定义种子的方法)

这正确地将例如
人物
“Roel Reiné”作为
角色
“导演”链接到
电影
“Redbad”

剩余内容:如何获取电影的人物+角色列表

目前的状态是我可以获得人员(无需他们的角色):

修补匠:

>>> App\Movie::where('title','Redbad')->with('persons')->get()
=> Illuminate\Database\Eloquent\Collection {#3204
     all: [
       App\Work {#3224
         id: 1,
         name: "Redbad",
         persons: Illuminate\Database\Eloquent\Collection {#3179
           all: [
             App\Person {#3250
               id: 2,
               name: "Roel Reiné",
               pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3249
                 movie_id: 1,
                 person_id: 2,
                 role_id: 1,
               },
             },
    ...
这就给了《红魔》电影中的相关人员一个机会。在pivot对象中,我看到了角色id。
它允许我进行诸如
$movie->persons()->where('role_id',4)->get()
之类的查询,这为我提供了特定电影或电影的所有
演员`

但我更喜欢像人物一样,急切地加载角色,这样我就可以轻松地创建一个电影列表,包括人物和他们的角色。例如

movie A - person K - role director
movie A - person L - role director
movie A - person L - role actor
movie B - person M - role director
movie B - person N - role actor

定义此关系所需的表:

users
    id - integer
    name - string
    ...

roles
    id - integer
    name - string
    user_id - integer
    movie_id - integer

movies
    id - integer
    title - string
实施

让我们在用户模型上定义movies方法

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function movies()
    {
        return $this->belongsToMany('App\Movie', 'roles', 'user_id', 'movie_id')->withPivot('name');
    }
}

在我的例子中,
角色
是“演员”、“导演”等。我从你的回答中感觉到你认为这是角色的名字。你的回答确实帮助我取得了进步,但我还没有做到。我已经更新了原始帖子以提供更多细节。具体来说,你的答案有一个表,其中存储了一个角色名,并将其与一部电影和一个人链接起来。我需要链接3个表:1)电影,2)人物和3)角色(导演/编剧/演员/等),并有一种“雄辩”的方式来检索电影中的人物+角色。我想你需要的是
之间的关系。如果我没有弄错的话,如果我有
电影人的兴趣爱好
,并且想了解电影中涉及的人的兴趣爱好,这将是适用的。我想要的是:电影中的人物和他们的角色。
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function movies()
    {
        return $this->belongsToMany('App\Movie', 'roles', 'user_id', 'movie_id')->withPivot('name');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Movie extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany('App\User', 'roles', 'movie_id', 'user_id')->withPivot('name');
    }
}
$user = App\User::find(1);

foreach ($user->movies as $movie) {
    // You may access the intermediate table using the pivot attribute on the models:
    echo $movie->pivot->name; // retrieve the role name for user #1 in $movie
}