How to pass a query to Eloquent?

Asked

Viewed 99 times

1

I’m trying to translate a SQL for Eloquent, where she receives an id ($myid) and returns the combinations of clothing, IE, I have a table clothes and I have another table combinations_clothes and in this case I have a relationship many to many.

The table combinations_clothes is constituted as follows::

Schema::create('combinations_clothes', function (Blueprint $table) 
{
        $table->increments('id');
        $table->integer('clothe_id')->unsigned();
        $table->foreign('clothe_id')->references('id')
                        ->on('clothes')->onDelete('cascade');

        $table->integer('combination_id')->unsigned();
        $table->foreign('combination_id')->references('id')
                        ->on('clothes')->onDelete('cascade');
}

To that end, I created a SQL to return the combinations of clothing ($myid), regardless of the order with which it was attached, i.e. clothe_id = 2 and combination_id = 5 is the same as being recorded clothe_id = 5 and combination_id = 2 and the SQL is the following:

select 'clothes.id´ from clothes join 'combinations_clothes´
where (clothes.id = clothe_id or clothes.id = combination_id)
        and clothes.id <> $myid

However, I needed to pass on this SQL for Eloquent of Laravel. Can someone help me?

  • 1

    You have both classes?

  • I only have class Clothes. Since it is a relation Many to Many between the same table 'Clothes', I used the model Clothes and created the methods of the relation Many to Many within this.

  • That answer is a comment, if you can put here or direct in your question, make available the class Clothes and if you’re related to them too!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.