dongshiru5913 2015-08-04 08:08
浏览 168
已采纳

Laravel一些问题(分页,如果 - foreach)

I'm laravel newbie. I'm created simple code and I have some questions: I think this code bad (it works, but I use @forelse($forums as $forum) and anywhere use $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

And second question how to make pagination? I'm tried this:

<?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)
        ]);
    }
}

But not work's, I'm tried tutorials..etc, how to? Thanks so much in advance ! :)

  • 写回答

4条回答 默认 最新

  • doudiecai1572 2015-08-04 08:15
    关注

    for your first question. You can use @foreach or @each. these are the two that i usually used. for your second question:

    return View::make('forum', [
                'forums'    => DB::table('forums')
                    ->where('id', $fid)
                    ->where('seo_name', $fname)
                    ->select()
                    ->paginate(5);
            ]);
    

    remove ->get()

    and replace simplePagination(5) with paginate(5)

    documation http://laravel.com/docs/5.0/pagination

    Update

    change you code block from

    return View::make('forum', [
                'forums'    => DB::table('forums')
                    ->where('id', $fid)
                    ->where('seo_name', $fname)
                    ->select()
                    ->paginate(5);
            ]);
    

    to

    $forums = DB::table('forums')
                        ->where('id', $fid)
                        ->where('seo_name', $fname)
                        ->select()
                        ->paginate(5);
    return View::make('forum', compact('forums'));
    

    then check if $forums->render() got error.

    Update

    $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'));
    

    on your view you do <?php echo $topics->render() ?> since topic is the one you paginate. also you can remove ->select() from your code. if you don't specify fields to output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真