Masuma Sultana Masuma Sultana - 3 months ago 13
PHP Question

Pagination not working in laravel 5.1 gives me all the data

I am currently doing a project called Doctor Management System.In this project I am stuck because my pagination is not working.I want to show 2 hospital info per page but it gives me all the data.Please help me solving this.
My Route file is

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

My controller is

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


My view file is

<?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


District Model is

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


Hospital model is

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

Answer

You should write this in your controller

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

This will solve your problem