n0tttrui
3 years ago
628
20
Eloquent

Select movies where category...

Posted 3 years ago by n0tttrui

I have rather a category system:

#Movie.php (Model)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Movie extends Model
{
    /**
    **/

    public function categories() {
        return $this->belongsToMany(Category::class);
    }

    public function thumbnail() {
        return url();
    }

    public function comments() {
        return $this->hasMany(MovieComment::class);
    }

}

#Category.php (Model)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function movies() {
        return $this->belongsToMany(Movie::class);
    }

    
}

#Controller

 public function filme($id)
    {

        $movie = Movie::with('comments', 'categories')->where('imdb_id', $id)->first();
        //dd($movie);
        return view('movie.see', compact('movie'));

    }

#I wanted to know how can I build a query in the Eloquent go get movies that have the same categories as this?

Please sign in or create an account to participate in this conversation.