user7424490
user7424490

Reputation:

Laravel 8: How to insert data into One To One relationship

I have a project in Laravel 8 and in this project, I have One To One relationship between movies and imdbs tables.

So I have added these to Models:

Movie.php:

public function imdb()
    {
        return $this->hasOne(Imdb::class);
    }

Imdb.php:

public function movie()
    {
        return $this->belongsTo(Movie::class);
    }

Then I created a form for adding movies. Here is the store method:

public function store(Request $request)
    {
        $new = new Movie();
        $new->name = $request->name;
        $new->link = $request->link;
        $new->year = $request->year;
        $new->actor = $request->actor;
        $new->director = $request->director;
        $new->imdb = $request->imdb;
        $new->starred = $request->starred;
        $new->watched = $request->watched;
        $new->save();
    }

As you can see $new->imdb = $request->imdb; is for inserting imdb rate but this data should be added to imdbs table and not movies table!

So, how can I do that?

Here is also imdbs table migration:

public function up()
    {
        Schema::create('imdbs', function (Blueprint $table) {
            $table->id();
            $table->string('rate');
            $table->integer('movie_id')->unsigned();
            $table->timestamps();
        });
    }

Update 1

         @foreach($movies  as $movie)
            <tr>
                <th scope="row">{{ $movie->id }}</th>
                <td>{{ $movie->name }}</td>
                <td>{{ $movie->year }}</td>
                <td>@mdo</td>
                <td>Mark</td>
                <td>{{ $movie->imdb->rate }}</td>
                <td>@mdo</td
                <td><a href="">Edit</a> | <a href="">Delete</a></td>
            </tr>
            @endforeach

And then this gives me:

ErrorException Trying to get property 'rate' of non-object

And $movies is coming from this method:

public function index()
    {
        $movies = Movie::all();
        return view('admin.movies.index', compact(['movies']));
    }

Upvotes: 0

Views: 2887

Answers (2)

Ballard
Ballard

Reputation: 899

On store of your Movie model, just make an IMDB model with the movie_id from the model you've just created.

I would suggest doing some validation of your request data too, which would look something like:

$data = $request->validate([
    //validation fields
]);

$movie = Movie::create($data);

Otherwise, the solution to your issue is:

    public function store(Request $request)
    {
        $movie = new Movie();
        $movie->name = $request->name;
        $movie->link = $request->link;
        $movie->year = $request->year;
        $movie->actor = $request->actor;
        $movie->director = $request->director;
        $movie->starred = $request->starred;
        $movie->watched = $request->watched;
        $movie->save();

        if(!is_null($movie)) {
            $imdb = new Imdb();
            $imdb->rate = $request->imdb;
            $imdb->movie_id = $movie->id;
            $imdb->save();
        }
    }

Upvotes: 2

JEJ
JEJ

Reputation: 904

delete column imbd from movies table then

 public function store(Request $request)
    { 
    $data = $request->only([
            'name','link','year','actor','director','starred','watched'
        ]);
     $movie=Movie::create($data);
     $imdb=$request->only(['imbd']);
     $imbd=$movie->imdb()->create($imbd);
}

Upvotes: 2

Related Questions