0

How can I get this query in laravel 5.1. I have model Menu, database menus. This query in SQL get results I want.

SELECT b.title FROM menus a, menus b WHERE a.id=b.parent_id 

Same thing, I can't get for this query

SELECT m.* FROM menus m WHERE m.id in (select m2.parent_id from menus m2)
Puck
  • 2,080
  • 4
  • 19
  • 30
tigrasti
  • 342
  • 3
  • 15

1 Answers1

1

Laravel docs for queries has an example that you can improvise on:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

Try to use something similar and note that you can use aliases:

$variable = DB::table('menus as a')
            ->join('menus as b', 'a.id', '=', 'b.parent_id')
            ->select('b.title')
            ->get();

Look at How to do this in Laravel, subquery where in for an example of where in equivalent.

Community
  • 1
  • 1
zedfoxus
  • 35,121
  • 5
  • 64
  • 63