laravel查询返回奇怪的顺序 [英] laravel query returns weird order
问题描述
因此,我在"错误概述"页面上遇到了一个错误. (多讽刺)..
从数据库中删除了一个错误(id : 7
被删除)后,这种奇怪的排序方式就变成了我的知识
So I came across a bug in my 'bugoverview' page. (how ironic)..
this weird way of order came to my knowledge after an bug was deleted out of the database (id : 7
was deleted)
无论如何,问题出在图片中:
Anyways the problem is as seen in the picture :
我将所有行都放在一个简单的foreach刀片中.
I get all the rows in a simple blade foreach.
我的查询代码是
$bugs_all = Bug::with('klant','user')->orderBy('id','asc')->get();
和
$projects_all = Project::all();
我的foreach代码:
my foreach code :
@foreach($projects_all as $project)
@foreach($bugs_all as $bug)
@if(count($bugs_all) > 0)
@if($bug->project_id == $project->id)
<tr>
<td>{{$bug->id}}</td>
<td>{{substr($bug->titel,0,15)}}...</td>
<td>{{$bug->status}}</td>
<td>{{$bug->soort}}</td>
<td>
@if($bug->prioriteit == 'laag')
<span class="label label-success">Laag</span>
@elseif($bug->prioriteit == 'gemiddeld')
<span class="label label-warning">Gemmideld</span>
@elseif($bug->prioriteit == 'hoog')
<span class="label label-danger">Hoog</span>
@elseif($bug->prioriteit == 'kritisch')
<span class="label label-purple">Kritisch</span>
@else
<span class="label label-info">Geen prioriteit</span>
@endif
</td>
<td>{{date('d-m-y - H:i',strtotime($bug->eind_datum))}}</td>
@if($bug->klant)
<td>{{$bug->klant->voornaam .' '.$bug->klant->tussenvoegsel.' '. $bug->klant->achternaam}}</td>
@endif
<td>{{$project->projectnaam}}</td>
@if($bug->user)
<td>{{$bug->user->voornaam .' '.$bug->user->tussenvoegsel.' '. $bug->user->achternaam}}</td>
@else
<td>Geen</td>
@endif
<td>
<a href="/bugchat/{{$bug->id}}" class="">
<button type="submit" class="btn btn-success btn-xs">
<i class="glyphicon glyphicon-search"></i>
</button>
</a>
</td>
</tr>
@endif
@endif
@endforeach
@endforeach
推荐答案
此问题包含一些歧义,没有看到ERD或您的表/关系结构,很难真正起到帮助作用.
This question contains some ambiguity, without seeing an ERD or your table/relationship structure it's hard to truely help.
但是,我假设您正在使用InnoDB,并且您没有正确订购关系.
However, I assume you are using InnoDB and you are not ordering your relationships correctly.
首先,InnoDB是一个引擎,它将重新使用已删除的空间.例如:创建记录1、2和3.然后删除记录2并创建记录4.默认顺序为 auto_increment (升序)(id
ASC并未明确地将其排序).
First of all InnoDB is an engine that will re-uses deleted space. For example: You create records 1, 2 and 3. Then you delete record 2 and create record 4. The default order is auto_increment in ascending order (this is not explicitly order by id
ASC).
这样,您的错误顺序如下:1,4,3-这仅仅是因为记录4替换了记录2(先前已删除)的空间.
With that said the order of your bugs are as follows: 1, 4, 3 - this is simply because record 4 replaced the space of record 2 (which was previously deleted).
第二,我只是建议您正确设置人际关系.我认为您的项目以某种方式与Bug有关?遵循以下内容即可:
Secondly I would simply advise you set-up your relationships correctly. I assume your Projects are related to Bugs somehow? Something along the following lines would suffice:
// Inside your Project model.
public function bugs()
{
return $this->hasMany('App\Bug', 'bug_id', 'id')
->orderBy('id', 'ASC');
}
// Inside your Bug model.
public function project()
{
return $this->belongsTo('App\Project', 'bug_id', 'id');
}
要获取所有项目的所有错误:
To get all bugs for all projects:
$projects = Project::with('bug.klant', 'bug.user')->all();
在刀片模板中,您可以执行以下操作:
Inside your blade template, you can do:
@foreach ($project->bugs as $bug)
{{ $bug->user }}
{{ $bug->klant }}
@endforeach
这篇关于laravel查询返回奇怪的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!