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 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)