samedi 23 juillet 2016

Laravel Route not working error is Trying to get property of non-object

I have now working on a project called Hospital & Doctor Information. Here I have different Division in Bangladesh & in each division there are districts in which there are certain types of hospital according to their ownership. But when I get the value of different hospital from it shows me some error. My route file

Route::get('/', array('as' =>'home' ,'uses' => 'UserController@index'));

Route::get('/about_us', array('as' =>'about_us' ,'uses' => 'UserController@about_us'));

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

My Controller is

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();
     $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('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">

                            <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->categories as $category)
                                    @foreach($dcategory->$hospitals as $hospital)
                                    <div class="col-xs-12 col-sm-6 blog-content">
                                        <h2></h2>
                                    </div>
                                    @endforeach
                                @endforeach
                            @endforeach


                    <ul class="pagination pagination-lg">
                        <li><a href="#"><i class="fa fa-long-arrow-left"></i>Previous Page</a></li>
                        <li class="active"><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">Next Page<i class="fa fa-long-arrow-right"></i></a></li>
                    </ul><!--/.pagination-->
                </div><!--/.col-md-8-->

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

& MY Model is Hospital.php

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

District.php

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

the error is enter image description here

Please Help Me. Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire