Masuma Sultana Masuma Sultana - 2 months ago 8
PHP Question

Routing did not work properly in laravel 5.1

I am currently doing a project called Doctor Management System.In this project I am stuck because my route did not work correctly.Whenevr I have to go on a specific id it gives me Gibberish id.
My Route file is

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


My Controller is

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Division;
use App\District;
use App\Category;
use App\Dcategory;
use App\Hospital;
use App\Doctor;
class UserController extends Controller
{


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

$district=District::find($id);
$category=Category::find($id);
$hospital=Hospital::find($id);


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


My view file is

<?php $active="hospital"; ?>


@extends('layouts.dashboard')
@section('content')

<section id="feature" class="transparent-bg">
<div class="container">
<div class="center wow fadeInDown">
<h2>Renowned Hospital in {{ $district->name }}</h2>
</div>

<div class="row">
<div class="features">
@foreach($categories as $category)
<div class="col-md-4 col-sm-6 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="feature-wrap">
<h2>{{ $category->name }}</h2>
<h3><a href="{!! URL::route('hospital_info') !!}"> View Details</a></h3>
</div>
</div><!--/.col-md-4-->
@endforeach


</div>
</div>
</div>
</section>


@stop

The error is:

enter image description here

Here You can see I did not find the id. But If I staticly Give the id it shows the Info. I don't find where the problem is. Please help me solving this.

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

And in your view file you should write

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

    <section id="feature" class="transparent-bg">
        <div class="container">
           <div class="center wow fadeInDown">
                <h2>Renowned Hospital in {{ $district->name }}</h2>
            </div>

            <div class="row">
                <div class="features">
                    @foreach($categories as $category)
                    <div class="col-md-4 col-sm-6 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
                        <div class="feature-wrap">
                            <h2>{{ $category->name }}</h2>
                            <h3><a  href="{!! URL::route('hospital_info',$district['id']) !!}"> View Details</a></h3>
                        </div>
                    </div><!--/.col-md-4-->
                    @endforeach


                </div>
            </div>
        </div>
    </section>
@stop