Mysql 将CodeIgniter查询转换为Laravel查询
我在CodeIgniter中有以下代码Mysql 将CodeIgniter查询转换为Laravel查询,mysql,laravel,codeigniter,query-builder,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($sqltou
$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);
预订路线如下所示(我的路线是预订我没有路线旅行):
最后,\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
印刷品
照亮\支持\收集对象([项目:受保护]=>阵列(
[0]=>stdClass对象([nTourTypeID]=>1[\u kftDescription]=>
巡航[\u kftColourID]=>003399)=>stdClass对象(
[nTourTypeID]=>2[\u kftDescription]=>4WD[\u KFTColor]=>)[2]=>
stdClass对象([nTourTypeID]=>3[\u kftDescription]=>Pearl Farm
[\u 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
@if(计数($tourTypes))
@foreach($tourType作为$tourType)
-
@if(计数($tourType->tours))
@foreach($tourType->tours as$tour)
- {{$tour->tddescription}
@endforeach
@恩迪夫
@endforeach
@恩迪夫
请注意,这个答案只是给你一个例子,说明你可以在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'));
});
Route::get('tours', function () {
$tourTypes = \App\Tourtype::with('tours')->get();
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
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');
}
}
希望这有帮助!请注意,这个答案只是给你一个例子,说明你可以在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'));
});
Route::get('tours', function () {
$tourTypes = \App\Tourtype::with('tours')->get();
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
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');
}
}
希望这有帮助!您会遇到什么样的错误?是否有错误消息?如果有,它会说什么?首先,我得到了
未定义的属性:illumb\View\Engines\CompilerEngine::$db(视图:C:\XAMPP\htdocs\bookings\resources\views\bookings\index.blade、 php)
然后将$tourtypes=$this->db->query($sqltourtypes)->result();
更改为$tourtypes=db::table('tours')->get()
现在我得到了这个eror:错误异常在e1cff7d5e9e0d2f02a08975fab510e441de69bc5.php第39行:未定义属性:stdClass:$\u kftDescription
只是无数不同的错误。我提到但无法运行查询如果不使用给定的工具,为什么要使用框架?在laravel中,正确的方法是定义模型d关系。控制器代码会简单得多。HTML代码应该移到模板中。我正在处理一个crud项目。我必须切换到laravel我有模型。除了这个查询,一切都很好。你使用的laravel版本是什么?你会得到什么样的错误?有错误消息吗?如果有,它会说什么?首先我得到了未定义的属性:illumb\View\Engines\CompilerEngine::$db(视图:C:\XAMPP\htdocs\bookings\resources\views\bookings\index.blade、 php)
然后将$tourtypes=$this->db->query($sqltourtypes)->result();
更改为$tourtypes=db::table('tours')->get()
现在我得到了这个eror:错误异常在e1cff7d5e9e0d2f02a08975fab510e441de69bc5.php第39行:未定义属性:stdClass:$\u kftDescription
只是无数不同的错误。我提到但无法运行查询如果不使用给定的工具,为什么要使用框架?在laravel中,正确的方法是定义模型d关系。控制器代码会简单得多。HTML代码应该移到模板中。我正在处理crud项目。我必须切换到laravel我有模型。除了这个查询,一切都很好。你使用的是什么版本的laravel?非常感谢你是最好的:)我确信它会工作,但我在未定义的变量tourTypes(视图:C:\XAMPP\htdocs\bookings\resources\views\bookings\index.blade.php…我是否放置了第一个代码Route::get('tours',function(){
在bookings\routes\module.php中?我不知道bookings\routes\module.php
是什么。我可以想象你会遇到这个错误,因为你没有将tourTypes
传递给你的视图,例如返回视图('bookings.index',compact('tourTypes');@jacker83将compact('tourTypes')
更改为compact('tourTypes'))
使其与变量名匹配。已更新…您能看到我更新的帖子吗?我仍然有未定义变量:tourTypes的错误。非常感谢您是最好的:)我确信它会工作,但我收到一个未定义变量:tourTypes的错误(视图:C:\XAMPP\htdocs\bookings\resources\views\bookings\index.blade.php…我是否放置第一个代码Route::get('tours',function()){
在bookings\routes\module.php中?我不知道bookings\routes\module.php
是什么。我可以想象你会遇到这个错误,因为你没有将tourTypes
传递给你的视图,例如返回视图('bookings.index',compact('tourTypes');@jacker83将compact('tourTypes')
更改为compact('tourTypes'))
以使其与变量名匹配。已更新…您能看到我更新的帖子吗?我仍然有未定义变量的错误:tourTypes。