在laravel 5.1中,路由无法正常工作

时间:2016-08-15 09:13:27

标签: php laravel laravel-5.1

我目前正在做一个名为医生管理系统的项目。在这个项目中,我被困住了,因为我的路线无法正常工作。我不得不继续使用特定的ID,它给了我Gibberish id。 我的路线文件是

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

我的控制器

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

我的观看文件是

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

@extends(&#39; layouts.dashboard&#39;) @section(&#39;内容&#39;)

<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

错误是:

enter image description here

在这里你可以看到我没有找到id。但是,如果我静态地给它ID,它会显示信息。我找不到问题所在。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

您在网址中使用{id},应该用整数ID替换。例如,您的网址应为http://localhost/doctor_info/public/district/hospital/hospital_info/1

答案 1 :(得分:0)

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

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

在你的视图文件中你应该写

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