Laravel 路由无效。没有拿到id

Laravel Route not working. Did not get The id

我现在正在从事一个名为医院和医生信息的项目。我在孟加拉国有不同的部门,每个部门都有一些地区,根据他们的所有权,有某些类型的医院。但是当我从中得到不同医院的价值时,它显示了一些 error.It 没有找到具体的 id.I 之前已经问过这些问题。但这并没有解决我的问题。所以我再问一次。我的路线文件

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

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

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

我的医院信息 Blade 文件是

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

                            <div class="col-xs-12 col-sm-4 blog-content">

                                <a href="#"><img class="img-responsive img-blog" src="images/2.jpg" width="100%" alt="" /></a>
                            </div>
                            @foreach($division->districts as $district)
                                @foreach($district->cats as $category)
                                    @foreach($category->hospitals as $hospital)
                                    <div class="col-xs-12 col-sm-6 blog-content">
                                        <h2>{{ $hospital->name }}</h2>
                                    </div>
                                    @endforeach
                                @endforeach
                            @endforeach
                            <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="#">Booking</a>
                            </div>
                        </div>    
                    </div><!--/.blog-item-->\
                </div><!--/.col-md-8-->

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

我的控制器是

public function district($id)
    {

        $divisions = Division::all();

        $division=Division::find($id); 

        $district=District::find($id); 

        // $districts=District::where('division_id', '=', $divisions->id)->get();

        if (!$division)
        {
            throw new NotFoundHttpException;
        }
        return view('users.district')
                    ->with('divisions',  $divisions)
                    ->with('division', $division)
                    ->with('district', $district);
    }


    public function hospital($id)
    {

         $divisions = Division::all();
         $division=Division::find($id); 

         $district=District::find($id); 

         $categories=Category::all();

        // $districts=District::where('division_id', '=', $divisions->id)->get();

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


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

         $district=District::find($id); 

         $categories=Category::all();
         $cats=Category::find($id); 
         $hospitals=Hospital::find($id); 

        // $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('cats',$cats)
                    ->with('hospitals',$hospitals);
    }

模范医院是

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

地区模型是

<?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\Categories');
    }
    public function dcategory()
    {
        return $this->hasmany('App\Dcategories');
    }

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

分区模型是

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;

class Division extends Model
{
    protected $fillable = ['name'];

    public function districts()
    {

        // $divisions = Division::all(); 
     //    $division_id = Input::get('id');
        return $this->hasMany('App\District','division_id');
    }
}

错误是

我几天前问过这个问题,但没有找到答案。请帮助我。

你的主要问题是你试图从 id 获取医院,但你没有在 Hospital 模型中定义任何 id 所以首先你应该将 id 列添加到你的 Hospital 模型。

其次改变你的功能

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

     $district=District::find($id); 

     $categories=Category::all();
     $cats=Category::find($id); 
     $hospitals=Hospital::find($id); 

    // $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('cats',$cats)
                ->with('hospitals',$hospitals);
}

对此

public function hospital_info($id)
{
    $hospital=Hospital::find($id);
    $divisions = Division::all();
    $categories=Category::all();


    // $districts=District::where('division_id', '=', $divisions->id)->get();

    // if (!$district)
    // {
    //     throw new NotFoundHttpException;
    // }


    return view('users.hospital_info') , compact('divisions','categories', 'hospital');
}

然后你可以像这样得到你的信息

                            <div class="col-xs-12 col-sm-6 blog-content">
                                <h2>{{ $hospital->name }}</h2>
                            </div>

             @foreach($divisions as $itemDivision)
                        @foreach($categories as $itemCategory)
                            <div class="col-xs-12 col-sm-6 blog-content">
                                <h2>{{ $itemDivision->name }}</h2>
                            </div>
                        @endforeach
                    @endforeach

要获取有关某个地区的所有信息,例如:

您应该将 (id) 列添加到您的所有模型中,以便稍后使用它们 在你的函数中这样做:

public function district($id)
{
    $district=District::findorfail($id); 
    $hospitals = DB::table('hospitals')
    ->leftjoin('districts' ,'hospitals.district_id' , '=', 'districts.id')
    ->where('district.id','=',$id)
    ->select('hospitals.*')
    ->get();



    return view('users.district'),compact('district','hospitals') ;
}

当然,医院是一个对象数组,您应该使用循环来获取每个医院的信息