I need to bring me the recipes that contain these categories, but this query brings me the categories with the recipes, which are related to these and I only need the recipes and that these are not repeated for each category, this is my query
$where = Recetas::with('categorias')->whereIn('nombre', ["Comida venga","Comida Carnivora"]);
And these are the tables is a many-to-many relationship.
The category table.
public function up()
{
Schema::create('categorias', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nombre')->unique();
$table->timestamps();
});
}
The Recipes table.
public function up()
{
Schema::create('recetas', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('nombre');
$table->string('dificultad');
$table->float('tiempo');
$table->smallInteger('comensales');
$table->text('descripcion');
$table->text('imagen');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
The pivot table.
public function up()
{
Schema::create('categorias_recetas', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('recetas_id');
$table->unsignedBigInteger('categorias_id');
$table->timestamps();
$table->foreign('categorias_id')->references('id')->on('categorias');
$table->foreign('recetas_id')->references('id')->on('recetas');
});
}
You can Query existence of relationship to get only the recipes that are related to a category where the name matches any of the elements in the array
["Comida venga", "Comida Carnivora"]
: