2017-03-05 91 views
0

我在笨轉換笨查詢Laravel查詢

$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

更新:我的應用程序\ HTTP \控制器\ 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','[email protected]'); 

最後\ resources \ views \ bookingings \ index.blade.php文件看起來像是th是:

@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); 

打印

照亮\支持\集合對象([項目:保護] =>數組( [0] => stdClass的對象([nTourTypeID] => 1 [_kftDescription] => 遊輪[_kftColourID] => 003399 )1 => stdClass Object( [nTourTypeID] => 2 [_kftDescription] => 4WD [_kftColourID] =>)[2] => stdClass對象([nTourTypeID] => 3 [_kftDescription] =>珍珠農場 [ _kftColourID] => 00ccff)

因此,該查詢正在工作,但無法打印ul和li標籤,其值爲using;

@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 
+0

你會得到什麼樣的錯誤?是否有錯誤訊息?如果是這樣,它說什麼? – waka

+0

首先我得到'未定義的屬性:Illuminate \ View \ Engines \ CompilerEngine :: $ db(View:C:\ XAMPP \ htdocs \ bookings \ resources \ views \ bookings \ index.blad e.php)''然後改變'$ tourtypes = $ this-> db-> query($ sqltourtypes) - > result();'to'$ tourtypes = DB :: table('tours') - > get();'現在我得到這個eror:'ErrorException在e1cff7d5e9e0d2f02a08975fab510e441de69bc5.php第39行:未定義的屬性:stdClass :: $ _ kftDescription只是無盡的不同的錯誤。我提到https://laravel.com/docs/4.2/queries,但無法運行查詢 – hijacker83

+0

爲什麼要使用框架,如果你不使用給定的工具? laravel的正確方法是定義模型和關係。控制器代碼會簡單得多。並且HTML代碼應該移到模板中。 –

回答

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

希望這有助於!

+0

非常感謝你是最好的:) – hijacker83

+0

我確定它會工作,但我得到一個錯誤未定義變量:tourTypes(查看:C:\ XAMPP \ htdocs \ bookings \ resources \ views \ bookings \ index.blade.php ... 我把第一個代碼'Route :: get('tours',function(){'in bookings \ routes \ module.php? – hijacker83

+0

我不知道'bookingings \ routes \ module.php'是什麼。我想你會遇到這個錯誤,因爲你沒有將'tourTypes'傳遞給你的視圖,例如返回視圖('bookings.index',compact('tourTypes'));. –