将 CodeIgniter 查询转换为 Laravel 查询

Converting CodeIgniter query to Laravel query

我在 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 和 tours。 _kftDescription用于列出ul标签下的旅游类型,tDescription用于列出特定组下的旅游名称作为li标签。

我在尝试转换查询时总是遇到错误。谁能建议如何从 CodeIgniter 实现我的代码?当 nTourTypeID 为“1”时,它们属于 Tour Type "Cruises"。希望是有道理的。

更新: 我的 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

我仍然收到错误

Undefined variable: tourTypes (View: D:\XAMPP\htdocs\bookings\resources\views\bookings\index.blade.php)

当我写作时

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

版画

Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [nTourTypeID] => 1 [_kftDescription] => Cruises [_kftColourID] => 003399 ) 1 => stdClass Object ( [nTourTypeID] => 2 [_kftDescription] => 4WD [_kftColourID] => ) [2] => stdClass Object ( [nTourTypeID] => 3 [_kftDescription] => Pearl Farm [_kftColourID] => 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

请注意,此答案只是为了举例说明您可以在 Laravel 中执行的操作。

假设你的 url 路线是 /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 广告会更有益,因为它可以减少 routes/controllers 中的代码,也有助于预先加载。

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

app/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');
    }
}

app/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

希望对您有所帮助!