Laravel 5 eloquent recursive programming

I have then next 2 tables


/* systems */
id sys_name
1 Debtors
2 Creditors
3 Cashbook

/* menus */
id system_id parent_id menu_desc
1 1 0 Maintenance
2 1 1 Maintain Debtors
3 1 2 Debtor Addresses
4 1 1 Maintain Clerks
5 3 0 Bank Reconciliation

I have 2 Eloquent models


/* System */
public function menus(){
return this->hasMany(Menu::class,'system_id','id')->where('menus.parent_id','=',0);
}
public function submenus(){
return this->hasMany(Menu::class,'system_id','id')->where('menus.parent_id','=','menus.id');
}

/* Menu */
public function system(){
return $this->belongsTo(System::class,'system_id','id');
}
public function parent(){
return $this->hasOne(Menu::class,'id','parent_id');
}
public function children(){
return $this->hasMany(Menu::class,'parent_id','id');
}

I want to achieve the following in blade.php:


@foreach($systems as $system)
.......
@foreach($menus as $menu)
.......
@foreach($submenus as $submenu)
........
@endforeach
@endforeach
@endforeach

with the following output: (I am using bootstrap for indentation)

Debtors
Maintenance
Maintain Debtors
Debtor Addresses
Maintain Clerks
Creditors
Cashbook
Bank Reconciliation

MenuController


$systems = System::with('menus','submenus')->get();

but it doesn’t work. There are no submenu records. I think the problem is ‘menus.id’ in function submenus() but dont know how to solve it.

Please help as I have battled more than 3 days to build a menu treeview.

Thanking you in advance

from Newest questions tagged laravel-5 – Stack Overflow http://ift.tt/1mxJIhN
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.