将CodeIgniter查询转换为Laravel查询

时间:2017-03-05 19:04:14

标签: mysql laravel codeigniter query-builder

我在CodeIgniter中有以下代码

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

        $tourtypes = $this->db->query($sqltourtypes)->result();
        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){
                $html[] = '<ul>';
                for($ia = 0; $ia < count($tours); $ia++){
                    $html[] = '<li>'.$tours[$ia]->tDescription.'</li>';
                }
                $html[] ='</ul></li>'; 
            }else {
                $html[] = '</li>';
            }   
        }
        return implode('',$html);

我最近不得不切换到Laravel框架。我无法让我的查询在Laravel中工作。基本上我有两个表,tourtypes和旅游。 _kftDescription用于列出ul标签下的旅游类型,tDescription用于将特定组下的旅游名称列为li标签。

尝试转换查询时总是出错。任何人都可以建议如何从CodeIgniter实现我的代码?其中nTourTypeID为“1”,它们属于巡航类型“巡航”。希望是有道理的。

enter image description here

更新:我的app \ Http \ Controllers \ BookingsController.php文件看起来像这样

namespace App\Http\Controllers;

use App\Models\Bookings;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Validator, Input, Redirect ; 
class BookingsController extends Controller {
    public function index()
    {
  $tourTypes = collect(DB::table('tourtypes')->orderBy('nTourTypeID')->get())
        ->map(function ($item) {
            $item->tours = DB::table('tours')->where('nTourTypeID', $item->nTourTypeID)->get();

            return $item;
        });

        return view('bookings', compact('tourTypes'));

    }

预订路线看起来像这样(我的路线是预订,我没有路线游览):

Route::get('bookings','BookingsController@getIndex');

最后\ resources \ views \ bookings \ index.blade.php文件如下所示:

@extends('layouts.app')

@section('content')
{{--*/ usort($tableGrid, "SiteHelpers::_sort") /*--}}
@if(count($tourTypes))

    <ul>
        @foreach($tourTypes as $tourType)
            <li>
                <a href="#">{{ $tourType->_kftDescription }}</a>

                @if(count($tourType->tours))
                    <ul>
                        @foreach($tourType->tours as $tour)
                            <li>{{ $tour->tDescription }}</li>
                        @endforeach
                    </ul>
                @endif
            </li>
        @endforeach
    </ul>

@endif

我仍然收到错误

  

未定义变量:tourTypes(查看:   d:\ XAMPP \ htdocs中\预订\资源\视图\预订\ index.blade.php)

写作时

$tourTypes = DB::table('tourtypes')->orderBy('nTourTypeID', 'ASC')->get();
print_r($tourTypes);

打印

  

Illuminate \ Support \ Collection Object([items:protected] =&gt;数组(   [0] =&gt; stdClass对象([nTourTypeID] =&gt; 1 [_kftDescription] =&gt;   游轮[_kftColourID] =&gt; 003399)1 =&gt; stdClass对象(   [nTourTypeID] =&gt; 2 [_kftDescription] =&gt; 4WD [_kftColourID] =&gt; )[2] =&gt;   stdClass对象([nTourTypeID] =&gt; 3 [_kftDescription] =&gt;珍珠养殖场   [_kftColourID] =&gt; 00ccff)

所以,查询正在运行,但我无法使用值打印ul和li标签;

@if(count($tourTypes))
    <ul>
        @foreach($tourTypes as $tourType)
            <li>
                <a href="#">{{ $tourType->_kftDescription }}</a>
                @if(count($tourType->tours))
                    <ul>
                        @foreach($tourType->tours as $tour)
                            <li>{{ $tour->tDescription }}</li>
                        @endforeach
                    </ul>
                @endif
            </li>
        @endforeach
    </ul>
@endif

1 个答案:

答案 0 :(得分:2)

请注意,这个答案只是为了举例说明你在Laravel中可以做些什么。

说出您的路线网址为/tours,您可以执行以下操作:

Route::get('tours', function () {

    $tourTypes = collect(DB::table('tourtypes')->orderBy('nTourTypeID')->get())
        ->map(function ($item) {
            $item->tours = DB::table('tours')->where('nTourTypeID', $item->nTourTypeID)->get();

            return $item;
        });

    return view('tours', compact('tourTypes'));
});

然后创建文件resources/views/tours.blade.php并添加以下内容:

@if(count($tourTypes))

    <ul>
        @foreach($tourTypes as $tourType)
            <li>
                <a href="#">{{ $tourType->_kftDescription }}</a>

                @if(count($tourType->tours))
                    <ul>
                        @foreach($tourType->tours as $tour)
                            <li>{{ $tour->tDescription }}</li>
                        @endforeach
                    </ul>
                @endif
            </li>
        @endforeach
    </ul>

@endif

以上示例仅输出ul。本教程应该对您有所帮助: https://laracasts.com/series/laravel-5-from-scratch/episodes/5

此外,正如@PaulSpiegel在评论中提到的那样,使用Eloquent广告会更有利于减少路由/控制器中的代码,并有助于加载。

为此,您可以创建以下文件:

应用程序/ Tour.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tour extends Model
{
   protected $primaryKey = 'kpnID';

    public function Tourtypes()
    {
        return $this->belongsTo(Tourtype::class, 'nTourTypeID', 'nTourTypeID');
    }
}

应用程序/ Tourtype.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tourtype extends Model
{
    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'nTourTypeID';

    /**
     * Tours Relationship
     * 
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function tours()
    {
        return $this->hasMany(Tour::class, 'nTourTypeID', 'nTourTypeID');
    }
}

在上面我假设游览的主键是kpnID。如果不是那么就改变它。

然后你的路线看起来像:

Route::get('tours', function () {

    $tourTypes = \App\Tourtype::with('tours')->get();

    return view('tours', compact('tourTypes'));
});

https://laravel.com/docs/5.1/eloquent

https://laravel.com/docs/5.1/eloquent-relationships#one-to-many

https://laravel.com/docs/5.1/blade#defining-a-layout

希望这有帮助!