gpt4 book ai didi

mysql - 将 CodeIgniter 查询转换为 Laravel 查询

转载 作者:行者123 更新时间:2023-11-29 07:03:15 25 4
gpt4 key购买 nike

我在 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 中进行查询。基本上我有两张 table ,tourtype 和tours。 _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

我仍然收到错误

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 中执行哪些操作。

假设您的路线网址是 /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 广告对您来说会更有利,它可以减少路由/ Controller 中的代码,并且还有助于提前加载。

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

应用程序/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

希望这有帮助!

关于mysql - 将 CodeIgniter 查询转换为 Laravel 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42612683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com