Daybreak
Daybreak

Reputation: 165

Laravel Eloquent Relationship - Inserting data into multiple tables

I have the following structure:

3 tables: movies, actors, genres

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:

id: 1 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 :/

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:

enter image description here

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.

Upvotes: 5

Views: 4429

Answers (1)

user320487
user320487

Reputation:

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

Upvotes: 4

Related Questions