Ad
How To Do Self Join In Query Builder No Eloquent
Having hard time to do self join with query builder but getting unexpected results or query while using toSql()
or get()
, please don't suggest eloquent, just query builder with raw query.
Trying to get all child menus followed by their parent. Id column also required
$users = DB::table('all_menus as A all_menus as B')
->select(DB::raw('A.menu_name__v1 As menu_name_parent, B.menu_name__v1 As menu_name_child'))
->where('A.id', '=' 'B.parent_menu_id__v1')
->toSql();
Ad
Answer
join()
method supports to define an alias name, so you can self join with alias name like this query:
$users = DB::table('all_menus as A')
->join('all_menus as B', 'A.id', '=', 'B.parent_menu_id__v1')
->select('A.menu_name__v1 AS menu_name_parent', 'B.menu_name__v1 AS menu_name_child')
->toSql();
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad