我是一个旅游新手。我创建了简单的代码,我有一些问题:我认为这段代码不好(它工作,但我使用@forelse($forums as $forum)
和任何地方使用$forum)
@extends('layouts.main')
@section('content')
@forelse($forums as $forum) <-- I don't like this line, it works but i think it's possible with if or something else
@forelse($topics as $topic)
{{ $topic->title }}<br>
@empty
Sorry but this forums empty.
@endforelse
@empty
Sorry but this forum not found
@endforelse
@stop
第二个问题是如何进行分页?我试过了:
<?php
namespace AppHttpControllers;
use DB;
use View;
class viewForum extends Controller
{
public function showForum($fname, $fid)
{
return View::make('forum', [
'forums' => DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->get()
->simplePagination(5)
]);
}
}
但不是工作的,我试过教程…等等,该怎么做?提前感谢!:)
回答你的第一个问题。您可以使用@foreach或@each。这是我常用的两种。第二个问题:
return View::make('forum', [
'forums' => DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->paginate(5);
]);
remove ->get()
并将simplePagination(5)替换为paginate(5)
documation http://laravel.com/docs/5.0/pagination
从
更改代码块return View::make('forum', [
'forums' => DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->paginate(5);
]);
$forums = DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->paginate(5);
return View::make('forum', compact('forums'));
然后检查$forums->render()是否出错。
$forums = DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->get(5);
$topics = DB::table('topics')
->where('forum_id', $id)
->select()
->paginate(2)
return View::make('forums', compact('forums', 'topics'));
在您的视图中,您执行<?php echo $topics->render() ?>
,因为主题是您分页的主题。还可以从代码中删除->select()。如果没有指定要输出的字段。
For @foreach ($topics as $topic)
{{$topic->title}}
@endforeach
分页的$users = User::where('status','1')->(10)分页;
注意:在View中添加{{$user->links()}}用于获取分页链接
你可以使用@foreach() @endforeach或者@if @else @endif参考范例:
@foreach($forums as $forum)
@if($forum==0)
{{'empty'}}
@else
{{ 'Not empty' }}
@endif
@endforeach
分页我建议您使用jquery datatable进行适当的分页。这很好,而且节省了很多时间。参见下面的示例实现:
//this preload the jquery library for datatable together with the print button
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>
//this section call the document ready event making sure that datatable is loaded
<script>
$(document).ready(function() {
$('#').DataTable();
} );
//this section display the datatable
$(document).ready(function() {
$('#mytable').DataTable( {
dom: 'Bfrtip',
"pageLength": 5, //here you can set the page row limit
buttons: [
{
extend: 'print',
customize: function ( win ) {
$(win.document.body)
.css( 'font-size', '10pt' )
.prepend(
''
);
$(win.document.body).find( 'table' )
.addClass( 'compact' )
.css( 'font-size', 'inherit' );
}
}
]
} );
} );
</script>
//you can display record on the datatable as shown below
<div class="table-responsive col-md-12">
<table id="mytable" class="table table-bordered table-striped table-highlight">
<thead>
<tr bgcolor="#c7c7c7">
<th>S/N</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@php
$i=1;
@endphp
@foreach($queryrecord as $list)
<tr>
<td>{{ $i++ }}</td>
<td>{{ $list->name }}</td>
</tr>
@endforeach
</tbody>
</table>
<hr />
</div>
注意:在显示数据表上的信息之前,必须先从数据库中查询记录。我在这里使用查询生成器作为示例
$data['queryrecord']=DB::table('tablename')->get();