Sunday, March 5, 2017

converting codeigniter query to laravel query

I have the following code in codeigniter

$html = array();
        $sqltourtypes = 'SELECT * FROM tourtypes ORDER BY nTourTypeID ASC';
        //print $sql; exit();
        $sqltours = 'SELECT * FROM tours WHERE nTourTypeID = ? ORDER BY _kpnID ASC';

        $tourtypes = $this->db->query($sqltourtypes)->result();
        //$tourtypes->num_rows();
        for($i = 0; $i < count($tourtypes); $i++){
            $html[] = '<li><a href="#">'.$tourtypes[$i]->_kftDescription.'</a>';
            $tours = $this->db->query($sqltours,array($tourtypes[$i]->nTourTypeID))->result();
            if(count($tours)>0){
                //$tours->num_rows();
                $html[] = '<ul>';
                for($ia = 0; $ia < count($tours); $ia++){
                    $html[] = '<li>'.$tours[$ia]->tDescription.'</li>';
                }
                $html[] ='</ul></li>'; 
            }else {
                $html[] = '</li>';
            }   
        }
        return implode('',$html);

I recently had to switch to Laravel framework. I couldn't make my query work in laravel. Basicly I have two tables. tourtypes and tours... _kftDescription is used to list the tour types under the ul tags and tDescription is used to list tour names under the specific group as a li tags. I always get an error when trying to convert the query. Can anyone suggest me how to implement my code from codeigniter? Where nTourTypeID is 1 they belong to "Cruises" Tour Type. Hope makes sense. Thanks in advance.

enter image description here



via hijacker83

Advertisement