I have two tables one called " Events " and another one " Dependences ", a dependency can have several event , that's the relationship between they, what I try to do is show the data of a event , but also show the data of the dependence to which it is related.
This is my controller where I bring the event :
class WelcomeController extends Controller
{
public function inicio(){
$events = Event::orderBy('id', 'DESC')->where('status', 'PUBLISHED')->paginate(3);
return view('welcome', compact('events'));
}
this is the model of event :
class Event extends Model
{
protected $table = 'events';
protected $fillable = [
'admin_id', 'dependence_id', 'place_id', 'name', 'slug', 'excerpt', 'body', 'status', 'file'
];
public function dependences(){
return $this->belongsTo('App\Dependence', 'dependence_id');
}
dependence model:
class Dependence extends Model
{
protected $table = "dependences";
protected $fillable = [
'name', 'slug'
];
public function events(){
return $this->hasMany('App\Event');
}
so I try to show it in the view:
@foreach($event->dependences as $dependence)
<a href="#">
{{$dependence->name}}
</a>
@endforeach
but this error appears: Trying to get property 'name' of non-object
these are the tables in the database:
the routes are fine, but I do not know what is the problem, I hope your help, thank you very much.