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.It did not find the specific id.I Have asked the ques before. But it did not solve my problem. So I ask this again. My route file
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 Hospital Info Blade 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">
<img class="img-responsive img-blog" src="images/2.jpg" width="100%" alt="" />
</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>Share</h2>
<ul class="social-share">
<li><i class="fa fa-google-plus"></i></li>
<li><i class="fa fa-facebook"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-skype"></i></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
My Controller Is
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);
}
The Model Hospital 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');
}
}
The 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\Categories');
}
public function dcategory()
{
return $this->hasmany('App\Dcategories');
}
public function hospital()
{
return $this->hasmany('App\Hospital');
}
}
The Division model is
<?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');
}
}
The error is
I have asked the question a few days earlier but did not find the answer. Plz help me.
Your main problem is you're trying to get a hospital from id but you havn't defined any id in Hospital model so first thing first you should add id column to your Hospital model.
Secondly change your function from this
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);
}
To this
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');
}
then you can get your infromation like this
<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
To get all the information about a District for example :
You should add the (id) column to all your models for using them later
inside your function do this :
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') ;
}
Of course the hosptials is an array of objects and you should use a loop to get each hospital's informtaion
Related
Looking to solve this error that I'm getting when trying to display some data to the view. I'm working with v5.7 and I have a feeling it might be something with the index method in my controller, I could be very wrong. If there is any more info that is needed please let me know.
Trying to get property 'slug' of non-object (0)
Route:
Route::get('/category/{category}','BlogController#category')->name('category');
BlogCategory Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class BlogCategory extends Model
{
protected $fillable = ['title', 'slug'];
public function posts()
{
return $this->hasMany(Post::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
Post Model
public function category()
{
return $this->belongsTo(BlogCategory::class);
}
Controller:
protected $limit = 3;
public function index()
{
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = Post::with('author')
->latestFirst()
->published()
// ->filter(request()->only(['term','year','month']))
->simplePaginate($this->limit);
return view('pages.frontend.blog.index', compact('posts', 'categories'));
}
public function category(BlogCategory $category)
{
$categoryName = $category->title;
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = $category->posts()
->with('author')
->latestFirst()
->published()
->simplePaginate($this->limit);
return view("pages.frontend.blog.index", compact('posts', 'categories', 'categoryName'));
}
View:
#foreach ($posts as $post)
<article class="post-item">
#if ($post->image_url)
<div class="post-item-image">
<a href="{{ route('blog.show', $post->slug) }}">
<img src="{{ $post->image_url }}" alt="">
</a>
</div>
#endif
<div class="post-item-body">
<div class="padding-10">
<h2>
{{ $post->title }}
</h2>
{!! $post->excerpt_html !!}
</div>
<div class="post-meta padding-10 clearfix">
<div class="pull-left">
<ul class="post-meta-group">
<li>
<i class="fa fa-user"></i>
{{ $post->author->name }}
</li>
<li>
<i class="fa fa-clock-o"></i>
<time> {{ $post->date }}</time>
</li>
<li>
<i class="fa fa-folder"></i>
{{ $post->category->title }}
</li>
<li>
<i class="fa fa-comments"></i>
4 Comments
</li>
</ul>
</div>
<div class="pull-right">
Continue Reading »
</div>
</div>
</div>
</article>
#endforeach
Post table
Blog Cats table
The belongsTo function accepts a second argument for the name of the foreign key in your posts table, if you do not provide it the framework will try to guess what is the foreign key column name giving the name of the function as pattern, in your case category(), so the framework is searching for category_id, however, your foreign key column name is blog_category_id.
public function category()
{
return $this->belongsTo(BlogCategory::class, 'blog_category_id');
}
You should call the category relationship in the index of your controller, if this view is related to the index method!
public function index()
{
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = Post::with('author')
->category()
->latestFirst()
->published()
// ->filter(request()->only(['term','year','month']))
->simplePaginate($this->limit);
return view('pages.frontend.blog.index', compact('posts', 'categories'));
}
After I send the variable that contains the comments to the view, I can only display the user id. This is why I need to somehow foreach through all the comments and based on their user_id to add a new key-pair value with username-username and send it to the view afterwards. Unfortunately, I'm having trouble figuring how to do that.
public function specificImage($id){
$similarImages = null;
$image = Image::with('comments.user')->find($id);
$subImages = Image::where('parent_id', $id)->get();
$views = $image->views;
$image->views = $views + 1;
$image->save();
$authorId = $image->user_id;
$author = User::find($authorId);
$comments = Comment::where('image_id', $id)->get();
$recentImages = Image::where('parent_id', NULL)->where('user_id', $authorId)->orderBy('created_at', 'desc')->limit(9)->get();
$tag = Tag::whereHas('images', function($q) use ($id) {
return $q->where('taggable_id', $id);
})->first();
if (!empty($tag)) {
$tagId = $tag->id;
}
if (!empty($tagId)) {
$similarImages = Image::where('parent_id', NULL)->whereHas('tags', function($q) use ($tagId) {
return $q->where('tag_id', $tagId);
})->orderBy('created_at', 'desc')->limit(9)->get();
}
return view('specificImage', ['image' => $image,'subImages' => $subImages, 'recentImages' => $recentImages, 'similarImages' => $similarImages, 'author' => $author, 'comments' => $comments]);
}
Table:
Table: Comments
Columns: id, user_id, image_id, comment
Image model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function tags(){
return $this->morphToMany('App\Tag', 'taggable');
}
public function votes(){
return $this->hasMany('App\Vote');
}
public function comments(){
return $this->hasMany('App\Comment');
}
public function updateVotes()
{
$this->upvotes = Vote::where('image_id', $this->id)->where('vote', true)->count();
$this->downvotes = Vote::where('image_id', $this->id)->where('vote', false)->count();
$this->save();
}
public function updateComments()
{
$this->comments = Comment::where('image_id', $this->id)->count();
$this->save();
}
}
Comment model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function image(){
return $this->belongsTo('App\Image');
}
public function user(){
return $this->belongsTo('App\User');
}
}
User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function images(){
return $this->hasMany('App\Image');
}
public function comments(){
return $this->hasMany('App\Comment');
}
public function votes(){
return $this->hasMany('App\Vote');
}
public function roles(){
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
}
public function hasAnyRole($roles) {
if (is_array($roles)) {
foreach ($roles as $role) {
if ($this->hasRole($role)){
return true;
}
}
} else {
if ($this->hasRole($roles)){
return true;
}
}
return false;
}
public function hasRole($role){
if ($this->roles()->where('name', $role)->first()){
return true;
}
return false;
}
}
Blade
#extends('layouts.app')
#section('content')
<div class="specific-image-flexbox">
<div class="specific-image-column">
<div class='specific-image-container'>
<img class='specific-image' src='{{url("storage/uploads/images/specificImages/".$image->file_name)}}' alt='Random image' />
#foreach($subImages as $subImage)
<img class='specific-image' src='{{url("storage/uploads/images/specificImages/".$subImage->file_name)}}' alt='Random image' />
#endforeach
</div>
</div>
<div class="artwork-info-column">
<div class="artwork-info-container">
<p class='title'>{{ $image->name }}<p>
<p class='author'>на<a href='{{url("profile/".$author->username )}}'><span class='usernameA artwork-info-username-span'>{{$author->username}}</span></a><img class='artwork-info-profile-picture' src='{{url("storage/uploads/profile_pictures/edited/".$author->profile_picture)}}'></p>
#if(Auth::user())
#if(Auth::id() === $image->user_id || Auth::user()->hasRole('Admin'))
<a class='placeholderDelete' href='{{ route('deleteImage', ['image_id' => $image->id]) }}'><i class="far fa-trash-alt"></i> Изтрий изображението</a>
#endif
#endif
<p class='description'>{{ $image->description }}</p>
<p class='description'>Техника: {{ $image->medium }}</p>
<p><i class="far fa-eye"></i> {{ $image->views }} Преглеждания</p>
<p><i class="far fa-thumbs-up"></i> {{ $image->upvotes }} Харесвания</p>
<p class='commentsCount'><i class="far fa-comments"></i> {{ $image->comments }} Коментари</p>
<a class='downloadImage' href="{{url("storage/uploads/images/specificImages/".$image->file_name)}}" download="{{ $image->name }}"><i class="fas fa-file-download"></i> Изтегли</a>
<!--<a class='placeholderDelete fas fa-expand' href='{{url("storage/uploads/images/specificImages/".$image->file_name)}}'></a>-->
<div class='social-container'>
<div class="addthis_inline_share_toolbox"
data-url="{{ url()->full() }}"
data-title="{{ $image->name }} by {{ $author->username }}"
data-description="{{ $image->description }}"
data-media="{{url("storage/uploads/images/specificImages/".$image->file_name)}}">
</div>
</div>
#if(!empty($recentImages))
#if(count($recentImages) >= 9)
<p class='author'>Още произведения на<a href='{{url("profile/".$author->username )}}'><span class='usernameA artwork-info-username-span'>{{$author->username}}</span></a><img class='artwork-info-profile-picture' src='{{url("storage/uploads/profile_pictures/edited/".$author->profile_picture)}}'></p>
<div class="more-images-container">
#foreach($recentImages as $recentImage)
<div class="more-images-container-element">
<a href='{{url("image/".$recentImage->id)}}'>
<img class='more-images' src='{{url("storage/uploads/images/miniImages/".$recentImage->file_name)}}' alt='Random image' />
</a>
</div>
#endforeach
</div>
#endif
#endif
#if(!empty($similarImages))
#if(count($similarImages) >= 9)
<p class='similar-images'>Подобни произведения</p>
<div class="similar-images-container">
#foreach($similarImages as $similarImage)
<div class="similar-images-container-element">
<a href='{{url("image/".$similarImage->id)}}'>
<img class='more-images' src='{{url("storage/uploads/images/miniImages/".$similarImage->file_name)}}' alt='Random image' />
</a>
</div>
#endforeach
</div>
#endif
#endif
#auth
<div class='postComments'>
<form method='POST' action=''>
<textarea class='comment-section' name='comment'></textarea>
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
<input type="hidden" name="image_id" value="{{ $image->id }}">
<button class='postComment submit' type='submit' name='commentSubmit'>Изпрати</button>
</form>
</div>
#endauth
<div class='comments'>
#foreach($image->comments as $comment)
{{ $comment->user->username }}
#endforeach
</div>
</div>
</div>
</div>
<script>
var token = '{{ Session::token() }}';
var urlComment = '{{ route('comment') }}';
var urlLike = '{{ route('vote') }}';
</script>
#endsection
I would suggest adding the user relationship to your Comment model as well:
class Comment extends Model
{
public function image()
{
return $this->belongsTo('App\Image');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
You can then eager load the relationships and then access them in your blade file:
public function specificImage($id)
{
$image = Image::with('comments.user')->find($id);
return view('specificImage', ['image' => $image]);
}
Then in your blade file you would have something like:
#foreach($image->comments as $comment)
{{ $comment->user->username }}
#endforeach
Okay i'm trying get "likes" and "users" in Posts by relationship hasOne.
here is my Post.php Model
class Posts extends Model
{
protected $table = 'posts';
public function User()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function Like()
{
return $this->hasOne(Like::class, 'post_id', 'id');
}}
My Blade template
#foreach ($showdeals as $deal)
<div class="tab-pane active" id="home" role="tabpanel">
<div class="card-body">
<div class="profiletimeline">
{{$deal->like->status}}
<br>
{{$deal->user->email}}
<div class="sl-item">
<div class="sl-left"> <img src=" {{asset( '/assets/images/users/2.jpg')}}" alt="user" class="img-circle"> </div>
<div class="sl-right">
<div> {{$deal->user->username}} || {{$deal->subject}} <Br> <span class="sl-date">{{$deal->created_at}}</span>
<div class="m-t-20 row">
<div class="col-md-3 col-xs-12"><img src="{{$deal->image}}" alt="user" class="img-responsive radius"></div>
<div class="col-md-9 col-xs-12">
<p> {{$deal->body}} </p> עבור למוצר </div>
</div>
<div class="like-comm m-t-20"> 2 תגובות <i class="fa fa-heart text-danger"></i> 5 לייקים </div>
</div>
</div>
</div>
</div>
<hr></div>
</div>
#endforeach
And there is my Controller
class PostsController extends Controller
{
public function showdeals()
{
$showdeals = Posts::with( 'User', 'Like')->get();
return view('posts.show', compact('showdeals'));
}
public function helpnewview(){
return view('posts.anew');
}
public function helpnew(Request $request){
//User pick link
$userlink = $request['userlink'];
return \Redirect::route('newdeal', compact('userlink'));
}
public function new(Request $request)
{
//Emdeb user link
$link = Embed::create($request['userlink']);
$linke = $request['userlink'];
return view('posts.new', compact('link', 'userlink', 'linke'));
}
public function create(Request $request)
{
$posts = New Posts;
$posts->user_id = Auth::User()->id;
$posts->subject = $request['subject'];
$posts->body = $request['body'];
$posts->link = $request['link'];
$posts->price = $request['price'];
$posts->image = $request['image'];
$posts->tag = $request['tag'];
$posts->save();
return back();
}
}
Now if I do something like {{$deal->user->email}} its will work,
if I go to something like this {{$deal->like->status}} its does not work,
am I missing something ?
If you want multiple relationships to be eagerly loaded you need to use an array of relationships: Model::with(['rl1', 'rl2'])->get();
public function showdeals()
{
...
$showdeals = Posts::with(['User', 'Like'])->get();
...
}
EDIT:
From that json in the comments that I see, there is no attribute named status in your Like model so thats probably the root of the problem
Controller edit this code
public function showdeals()
{
$showdeals = Posts::all();
return view('posts.show', compact('showdeals'));
}
And blade file code
#foreach ($showdeals as $deal)
<div class="tab-pane active" id="home" role="tabpanel">
<div class="card-body">
<div class="profiletimeline">
{{ $deal->Like->status }}
<br>
{{ $deal->User->email }}
#endforeach
I think everything is good except
{{$deal->like->status}} {{$deal->user->email}}
Please try as
{{$deal->Like()->status}}
<br>
{{$deal->User()->email}}
I do not know exactly how this works, so that's why I'm asking. I'm still working on a Bulletin Board System and now I want under each category, a subcategory.
For example:
I have done this using the following thread on LaraCasts:
https://laracasts.com/discuss/channels/eloquent/eloquent-getting-subcategories-from-the-categories
Now my IndexController looks like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Thread;
use App\Chat;
use App\Category;
class IndexController extends Controller
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$threads = Thread::all()->toArray();
$chats = Chat::orderBy('id', 'DESC')->take(50)->with('author')->get();
$categories = Category::with('sub_category')->get();
return view('index', compact('threads','chats','categories'));
}
}
This is my Category Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function sub_category()
{
return $this->belongsTo(self::class, 'parent_id', 'id');
}
public function parent_category()
{
return $this->hasMany(self::class, 'id', 'parent_id');
}
}
My view (index.blade.php):
#foreach($categories as $category)
<div class="col-md-8">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-angle-right" aria-hidden="true"></i> {{ $category->caption }}</div>
<div class="panel-body">
#foreach($category->sub_category as $sub_category)
<div class="row forum-row">
<div class="col-xs-12 col-md-8 col-sm-8">
<div class="hidden-xs visible-sm visible-lg visible-md pull-left forum-icon-read">
<i class="fa fa-comment"></i>
</div>
{{ $sub_category->caption }}<br>
<small>{{ $sub_category->desc }}</small>
</div>
<div class="col-xs-12 col-md-4 col-sm-4 forum-latest-container">
<p class="forum-data cutoff">Test</p>
<small>
door <a class="Donateur" href="/user-Sygun">Sygun</a>
<p class="forum-data">
2 minuten geleden </p>
</small>
</div>
</div>
#endforeach
</div>
</div>
</div>
#endforeach
I also have two database tables:
Sub_categories (id, caption, parent_id, min_rank, desc)
Categories (id, caption, min_rank, desc)
The error message I get:
Invalid argument supplied for foreach()
Can you tell me what is the right way to achieve this?
Create two model named Category and SubCategory.
In Category model
public function subcategories()
{
return $this->hasMany('App\SubCategory', 'parent_id');
}
In SubCategory model
public function parent_category()
{
return $this->belongsTo('App\Category', 'parent_id');
}
Then in your controller query it like:
$categories = Category::with('subcategories')->get();
And hopefully your foreach will work.
Category model
public function subcategories(){
return $this->hasMany('SubCategory', 'parent_id');
}
On view
#foreach($categories as $category)
#foreach($category->subcategories as $subcategory)
{{$subcategory->caption}}
#endforeach
#endforeach
And make sure you create SubCategory model.
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">
<img class="img-responsive img-blog" src="images/2.jpg" width="100%" alt="" />
</div>
#foreach($division->districts as $district)
#foreach($district->categories 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
<ul class="pagination pagination-lg">
<li><i class="fa fa-long-arrow-left"></i>Previous Page</li>
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>Next Page<i class="fa fa-long-arrow-right"></i></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
Please Help Me. Thanks in advance
In your view file, shouldn't it be like this ?
#foreach($division->$districts as $district)
#foreach($district->$categories as $category)
This row:
#foreach($dcategory->$hospitals as $hospital)
is wrong. The variable $dcategory does not exist and hospitals should be called like this:
#foreach($category->hospitals as $hospital)
Also you should check on your db if there are associated items, because maybe there are no hospitals associated to dcategory.