我是laravel新手。我创建了简单的代码,我有一些问题:
我认为这段代码不好(它有效,但我使用@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 App\Http\Controllers;
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)
]);
}
}
但是没有工作,我试过教程..等等,怎么样?非常感谢提前! :)
答案 0 :(得分:1)
第一个问题。你可以使用@foreach或@each。这些是我经常使用的两种。 对于你的第二个问题:
return View::make('forum', [
'forums' => DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->paginate(5);
]);
删除 - &gt; get()
并用paginate(5)
替换simplePagination(5)文件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-&gt; 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() ?>
,因为主题是您分页的主题。您也可以从代码中删除 - &gt; select()。如果您没有指定要输出的字段。
答案 1 :(得分:0)
对于@foreach($ topics为$ topic)
{{$topic->title}}
@endforeach
分页
$ users = User :: where('status','1') -> paginate(10);
注意:在View中添加此{{$ user-> links()}}以获得分页链接。
答案 2 :(得分:0)
您可以使用@foreach()@endforeach,也可以使用@if @else @endif 参见示例:
@foreach($forums as $forum)
@if($forum==0)
{{'empty'}}
@else
{{ 'Not empty' }}
@endif
@endforeach
答案 3 :(得分:0)
对于分页,我建议您使用jquery数据表进行正确的分页。很好,可以节省很多时间。参见下面的示例实现:
//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();