I got following error when I try to use this on my page
Trying to get property of non-object
I have two tables:
cvicenis
id
title
body
category_id
categories
id
name
My Cviceni model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cviceni extends Model
{
public function category()
{
return $this->belongsTo('App\Category', 'id');
}
}
My Category model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function cviceni()
{
return $this->hasMany('App\Cviceni', 'category_id');
}
}
My controller:
public function index()
{
$cviceni = Cviceni::all();
return view('excercises.index', compact('cviceni'));
}
My index.blade:
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-3">
<h1>Přehled cvičení</h1>
@if(count($cviceni) > 0)
<ul>
@foreach ($cviceni as $c)
<li> </li>
@endforeach
</ul>
@else
<h2>There is nothing to display</h2>
@endif
</div><!-- end of column -->
</div><!-- end of row -->
@endsection
If I use in my controller Cviceni::find(1) then I can get the value from $cviceni->category->name But not when I use a foreach loop. Can somebody help me please?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire