@n0tttrui you mean to get the movies that has a certain categorie? why not use
$categorie = Categorie::findOrFail($id);
$movies = $categorie->movies()->get();
otherwise you can explaine it a little bit, please.
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?
Call get() on the results.
$movies = Movie::whareHas('categories', function($query) use ($idCategories) {
$query->whereIn('id', $idCategories);
})->get();
Please or to participate in this conversation.