分页在 laravel 5.1 中不起作用给了我所有的数据

Pagination not working in laravel 5.1 gives me all the data

我目前正在做一个名为 Doctor Management 的项目 System.In 这个项目我卡住了,因为我的分页不是 working.I 想要每页显示 2 个医院信息,但它给了我所有 data.Please 帮我解决这个问题。 我的路线文件是

Route::get('/district/hospital/hospital_info/{id}', array('as' =>'hospital_info' ,'uses' => 'UserController@hospital_info'));

我的控制器是

public function hospital_info($id)
    {
        $divisions = Division::all();
         $division=Division::find($id); 

         $district=District::find($id); 
         $category=Category::find($id);
         $hospital=Hospital::find($id);
         $hospitals = $hospital->paginate(2);

        return view('users.hospital_info')
                    ->with('divisions',  $divisions)
                    ->with('division', $division)
                    ->with('district', $district)
                    ->with('category',$category)
                    ->with('hospitals',$hospitals);
    }

我的视图文件是

<?php $active="hospital"; ?>
@extends('layouts.dashboard')
@section('content') 

    <section id="blog" class="container">
        <div class="center">
            <h2>Hospital Information</h2>
            <h3 class="lead">The government approved a renowned hospital and improved quality of service address , doctor , patient viewing time, bed , pathological tests in various subjects including costs and find the information here .<br> The bed and cabin bookings online , pathological tests , the doctor can be a serial for the meeting from the app .</h3>
        </div>

        <div class="blog">
            <div class="row">
                 <div class="col-md-12">
                    <div class="blog-item">
                        <div class="row">
                            @foreach($district->hospital as $hospital)  
                                <div class="col-xs-12 col-sm-4 blog-content">
                                </div>

                                    <div class="col-xs-12 col-sm-6 blog-content">
                                        <h2>{{ $hospital->name }}</h2>
                                        <h5>Address : {{ $hospital->address }}</h5> 
                                        <h5>Ownership : {{ $hospital->ownership }}</h5>
                                        <h5>Start of activities : {{ $hospital->activities }}</h5>
                                        <h5>Number of beds : {{ $hospital->beds }}</h5>
                                        <h5>Number of doctor : {{ $hospital->doctors }}</h5>
                                        <h5>Phone No : {{ $hospital->phone }}</h5>
                                    </div>

                                <div class="col-xs-12 col-sm-2 blog-content">
                                    <h2><a href="#">Share</a></h2>
                                    <ul class="social-share">
                                            <li><a href="#"><i class="fa fa-google-plus"></i></a></li>
                                            <li><a href="#"><i class="fa fa-facebook"></i></a></li>
                                            <li><a href="#"><i class="fa fa-twitter"></i></a></li>
                                            <li><a href="#"><i class="fa fa-skype"></i></a></li>
                                        </ul>
                                        <br><br>

                                    <a class="btn btn-success readmore" href="{!! URL::route('doctor_list') !!}">Doctor List</a>
                                </div>
                                @endforeach 
                            </div>

                    </div><!--/.blog-item-->
                    @if($hospitals->lastPage() > 1)

                        <ul class="pagination pagination-lg">
                        {!! $hospitals->render() !!}

                        </ul><!--/.pagination-->
                    @endif

                </div><!--/.col-md-8-->

            </div><!--/.row-->
        </div>
    </section><!--/#blog-->
@stop

地区模型是

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class District extends Model
{
    protected $fillable=[
        'name',
        'division_id',
    ];



    public function division()
    {
        return $this->belongsTo('App\Division');
    }

    public function category()
    {
        return $this->hasmany('App\Category');
    }
    public function dcategory()
    {
        return $this->hasmany('App\Dcategories');
    }

    public function hospital()
    {
        return $this->hasmany('App\Hospital');
    }
    public function doctor()
    {
        return $this->hasmany('App\Doctor');
    }
}

医院模型是

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Hospital extends Model
{

    protected $fillable = [
        'name',
        'division_id',
        'district_id',
        'category_id',
    ];

    public function district()
    {

        return $this->belongsto('App\District');
    }
    public function division()
    {

        return $this->belongsto('App\Division');
    }
    public function category()
    {

        return $this->belongsto('App\Category');
    }
}

find returns 单个 eloquent 对象,paginate 需要一个集合才能工作:

$hospitals = Hospital::where('your_id_field', $id)->paginate(2);

试试这个

$hospitalsInDistrict = Hospital::where('district_id',$id)->paginate(2);

你应该在你的控制器中写这个

public function hospital_info($id)
    {
        $divisions = Division::all();
         $division=Division::find($id); 

         $district=District::find($id);
         $categories=Category::all(); 
         $category=Category::find($id);
         $hospital=Hospital::find($id);
         // $hospitals =$hospital->paginate(5);
         $hospitals = Hospital::where('district_id',$id)->paginate(2);


         $doctor=User::find($id);
         $doctors = User::where('district_id',$id)->paginate(1);
        // $districts=District::where('division_id', '=', $divisions->id)->get();

        // if (!$district)
        // {
        //     throw new NotFoundHttpException;
        // }
        return view('users.hospital_info')
                    ->with('divisions',  $divisions)
                    ->with('division', $division)
                    ->with('district', $district)
                    ->with('categories',$categories)
                    ->with('category',$category)
                    ->with('hospital',$hospital)
                    ->with('hospitals',$hospitals)
                    ->with('doctor',$doctor)
                    ->with('doctors',$doctors);
    }

这将解决您的问题