简体   繁体   English

Laravel Eloquent Relationship - 将数据插入多个表格

[英]Laravel Eloquent Relationship - Inserting data into multiple tables

I have the following structure: 我有以下结构:

3 tables: movies, actors, genres 3桌:电影,演员,流派

Movies Table Schema: 电影表架构:

Schema::create('movies', function (Blueprint $t) {
        $t->increments('id');
        $t->string('title');
        $t->integer('genre_id')->unsigned();
        $t->foreign('genre_id')->references('id')->on('genres');
        $t->string('genre');
        $t->timestamps();
    });

Genres Table Schema: 类型表架构:

Schema::create('genres', function (Blueprint $t) {
        $t->increments('id');
        $t->string('name');
        $t->timestamps();
    });

Actors Table Schema: 演员表架构:

Schema::create('actors', function (Blueprint $t) {
        $t->increments('id');
        $t->string('name');
        $t->timestamps();
    });

Movie Modal: 电影模态:

public function genre()
{
    return $this->belongsTo('App\Genre');
}
public function actor()
    {
        return $this->belongsToMany('App\Actor');
    }

Genre Modal: 类型莫代尔:

public function movie()
{
    return $this->hasMany('App\Movie');
}

Actor Modal: 演员莫代尔:

public function movie()
{
    return $this->belongsToMany('App\Movie');
}

Form: 形成:

<form method="post" action="{{ route('movies.insert') }}">
   <input type="text" name="movieName" id="movieName">
   <input type="text" name="actorName" id="actorName">
   <input type="text" name="genre" id="genre">
   <button type="submit">Add</button>
</form>

I am using the following controller method to post data from the form and everything is working fine but when I submit 2 movies with the same Genre, example "Action/Drama", I am getting 2 separate entries in the genres table like: 我使用以下控制器方法从表单发布数据,一切正常,但当我提交2个具有相同类型的电影,例如“动作/戏剧”时,我在流派表中得到2个单独的条目,如:

id: 1 name: Action/Drama id:1 name:Action / Drama

id: 2 name: Action/Drama id:2 name:Action / Drama

What is the best method to use a single id for a specific genre type over and over again? 对于特定类型类型反复使用单个ID的最佳方法是什么? For example, if I add 10 movies with genre type 'Action/Drama', then the 'genre_id' foreign key in the 'movies' table should only show one specific id which corresponds with the genres table's 'Action/Drama' Id. 例如,如果我添加10个流派类型为“动作/剧情”的电影,那么“电影”表中的“genre_id”外键应该只显示一个特定的id,它与流派表的“动作/戏剧”ID相对应。 Hope that makes sense :/ 希望有道理:/

Controller Method: 控制器方法:

public function addMovies(Request $request)
{
    $genre = new Genre;
    $genre->name = $request->input('genre');
    $genre->save();

    $movie = new Movie;
    $movie->title = $request->input('movieName');
    $movie->genre = $request->input('genre');
    $movie->genre()->associate($genre);
    $movie->save();

    $actor = new Actor;
    $actor->name = $request->input('actorName');
    $actor->save();
    $actor->movie()->save($movie);

    return redirect()->route('movies.search');
}

The output table should look something like this: 输出表应如下所示:

在此输入图像描述

Note: I also have a pivot table which is connecting movies with actors to facilitate the many-to-many relation, but I haven't included it here. 注意:我还有一个数据透视表,它将电影与演员连接以促进多对多关系,但我没有将它包括在这里。

Well you are explicitly saving a new genre every time that's why you're getting duplicates. 那么你每次明确地保存一个新的类型,这就是为什么你得到重复。 What you want to do is something like 你想做的就是这样

$genre = Genre::firstOrCreate("name", $request->input('genre'));

Then you'd assign the movie to the genre 然后你将电影分配到流派

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM