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

时间:2022-01-14 07:55:49

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? 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. Hope that makes sense :/

对于特定类型类型反复使用单个ID的最佳方法是什么?例如,如果我添加10个流派类型为“动作/戏剧”的电影,那么“电影”表中的“genre_id”外键应该只显示一个与流派表的“动作/戏剧”ID相对应的特定ID。希望有道理:/

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:

输出表应如下所示:

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

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.

注意:我还有一个数据透视表,它将电影与演员连接以促进多对多关系,但我没有在此处包含它。

1 个解决方案

#1


4  

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

然后你将电影分配到流派

#1


4  

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

然后你将电影分配到流派