Laravel-使用created_at页面排序的分页

时间:2019-07-15 22:30:03

标签: laravel pagination

所以我正在研究这个项目,需要laravel分页。我已经尝试过在刀片上使用$posts->links()进行laravel基本分页,但无法正常工作。我认为有些更新或其他内容。我需要分页才能由created_at分页,因此在一页上它需要显示我在同一天创建的所有帖子。你能帮我吗

这是我的HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Post;
use DB;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        return view('home')->with('posts', $user->posts);
    }


}

这是我的home.blade.php

@extends('layouts.theme')

@section('content')
    <style>
        body{
            margin: 0;
            background-color: #EBEBEB;
        }
    </style>
    <div class="mainl">
        <div class="row">
            <div class="columna">



                <h1>{{ Auth::user()->name }}</h1>
                <hr>
            </div>

            <div class="columns">
                <a href="{{ route('logout') }}" id="logout"
                onclick="event.preventDefault();
                        document.getElementById('logout-form').submit();">
                    {{ __('LOGOUT') }}
                </a>

                <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                    @csrf
                </form>

            </div>
        </div>
        </br></br></br>

        @if(count($posts)> 0)
        <table>
            <thead>
                <tr>
                    <th>BR.KESICE</th>
                    <th>IME I PREZIME</th>
                    <th>BR.TELEFONA</th>
                    <th>POSAO</th>
                    <th>CIJENA</th>
                    <th>PLACANJE</th>
                    <th>POPUST</th>
                    <th>DATUM PREUZ.</th>
                    <th>DATUM IZDAV.</th>
                    <th>SMJENA</th>
                    <th>RADNIK</th>
                    <th>STATUS</th>
                    <th>IZMIJENI</th>

                </tr>
            </thead>
            <tbody>
                @foreach($posts as $post)
                <tr>
                    <td>{{$post->br_kesice}}</td>
                    <td>{{$post->ime}}</td>
                    <td>{{$post->br_telefona}}</td>
                    <td>{{$post->posao}}</td>
                    <td>{{$post->cijena}}</td>
                    <td>{{$post->placanje}}</td>
                    <td>{{$post->popust}}</td>
                    <td>{{$post->datum_preuz}}</td>
                    @if($post->status == 1)
                        <td>/</td>
                    @else
                        <td>{{$post->datum_izdav}}</td>
                    @endif
                    <td>{{$post->smjena}}</td>
                    <td>{{$post->radnik}}</td>
                    <td>
                        @if($post->status == 0)
                        <span class="label label-primary" id="statusdeaktivan">Deaktivan</span>
                        @elseif($post->status == 1)
                        <span class="label label-success" id="statusaktivan">Aktivan</span>
                        @elseif($post->status == 2)
                        <span class="label label-danger" id="statusdeaktivan">Rejected</span>
                        @else
                        <span class="label label-info" id="statusdeaktivan">Deaktivan</span>
                        @endif
                    </td>
                    @if($post->status == 3)

                    @else
                    <td><a href="posts/{{$post->id}}/edit" class="edit"><i class="far fa-edit"></i></a></td>
                    @endif

                </tr>

                @endforeach
            </tbody>
            <tfoot>
                <tr>
                    <th>UKUPAN IZNOS:&nbsp;&nbsp;{{ Auth::user()->posts->sum('cijena')}}&euro;</th>
                    <th>KARTICA:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Kartica')->sum('cijena')}}&euro;</th>
                    <th>GOTOVINA:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Gotovina')->sum('cijena')}}&euro;</th>
                    <th>VIRMAN:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Virman')->sum('cijena')}}&euro;</th>
                </tr>
            </tfoot>
        </table>

        @else
            <p>Trenutno nema unosa.</p>
        @endif
    </div>
@endsection

如果您需要其他帮助,请发表评论,我会添加它。

1 个答案:

答案 0 :(得分:0)

这是因为您没有对posts进行分页,而是全部。

您只需要将$user->posts更改为$user->posts()->paginate()即可使用。 (可选)您可以将一个数字传递给paginate方法,以告知您每页需要多少条记录。

希望有帮助!