skip to Main Content

I have two tables

1. menu

id      -     menu_name    -     menu_url
1       -     Home         -      index
2       -     About        -      about
3       -     Services     -            
4       -     Contact      -      contact

2. submenu 

id      -     menu_name    -     submenu_name  
1       -     Services     -      Web Development
2       -     Services     -      Web Designing
3       -     Services     -      Logo Designing      
4       -     Services     -      SEO

for that i have created this query in php

$query = mysqli_query($con,"SELECT * FROM menu");
while ($row = mysqli_fetch_assoc($query)){
      if ($row['menu_url'] == ''){
        $subquery = mysqli_query($con,"SELECT * FROM submenu WHERE 
        menu_name = '".$row['menu_name']."'");
          while ($row1 = mysqli_fetch_assoc($subquery)){
            //do something here
           }
        }else{
           //do something here
        }
}

my question is that how do i do this query in laravel ?

my controller query

$menu = menu::all();

$submenu = submenu::where('menu_name','i want the $menu->menu_name variable here')->get();

how to get that variable in second query.

Thanks in advance any help will be appreciated.

5

Answers


  1. Try this:

    $menu=menu::all();
    
    if( count( $menu ) > 0 )  // Iterate only when there is some data available
    {
        foreach( $menu as $m )  // To get the individual records
        {
            $submenu=submenu::where('menu_name', $m->menu_name)->get();
    
            // here $submenu is an Std Class object, so use foreach() in the same way as above
        }
    }
    

    P.S.: Querying inside a loop is not a good idea.

    Login or Signup to reply.
  2. A more efficient way:

    $all_sub_menu = Submenu::get();
    
    $all_sub_menu = collect($all_sub_menu)->groupBy('menu_name');
    
    foreach( $menu as $m )  // To get the individual records
    {
    
        if(isset($all_sub_menu[$m->menu_name]){
            $sub_menu = $all_sub_menu[$m->menu_name]; 
            // you will get submenu for the current menu without requesting the database again and again
        }
        else{
            $sub_menu = array();
        }
    }
    
    Login or Signup to reply.
  3. Try below codes :

    number 1 :

    $menu=menu::all();
    
    $menu->each(
        function($eachMenu)
        {
            $submenu[] = submenu::where('menu_name', $eachMenu->menu_name)->get();
        }
    )
    

    number 2:

    $menu=menu::all();
    
    foreach($menu as $eachMenu) {
        $submenu[] = submenu::where('menu_name', $eachMenu->menu_name)->get();
    }
    
    Login or Signup to reply.
  4. I think it would be better if you create one-to-many relationship between Menu and Submenu models, in order to do this it would be better if you provide Foreign Key menu_id in sumbenu table;

    So you will have two tables with following columns:

    menu{id(primary_key), menu_name, menu_url(better to provide slug not url)};
    sumenu{id(primary_key), menu_id(foreign_key pointing to menu tables id), sumbenu_name, submenu_url(better to provide slug not url)}
    

    After you can define relationships in each model

    Menu Model:

    public function submenus()
        {
            return $this->hasMany('AppSubMenu');
        }
    

    SubMenu Model:

    public function menu()
        {
            return $this->belongsTo('AppMenu');
        }
    

    after all you can do $menuitems = Menu::all() , then pass the $menus variable to frontend view, and in frontend you can write

    @foreach($menuitems as $menuitem)
    {{ menuitem }}
        @foreach($menuitem->submenus as $submenu )
           {{ submenu }}
        @endforeach
    @endforeach
    

    That’s it 🙂

    Login or Signup to reply.
  5. Your first query (and loop) are unnecessary:

    SELECT x.* 
      FROM submenu x
      JOIN menu y
        ON y.menu_name = x.menu_name;
    

    I’ll leave the laravel bit as an exercise for the reader

    Also, as mentioned by others, you should use the PK of the menu table as the FK of the submenu table.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search