2017-03-05 109 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'));. –