Show data related to a foreach in Laravel?

2

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.

    
asked by Talked 16.09.2018 в 05:21
source

2 answers

2

It's simple, just confuse the type of data that returns you dependences of your model Event , I'll explain how:

When you have a function in your model that links to another, you have to take into account the type of data you return, in your case you have:

public function dependences(){
    return $this->belongsTo('App\Dependence', 'dependence_id');
}

Which is fine since according to your explanation an event belongs to a dependece and a dependency has several event , then when doing belongsTo (translated to: < strong> belongs to ) what you return is a objeto of the Dependence model.

Something similar happens in your Dependence model:

public function events(){
    return $this->hasMany('App\Event');
}

But in this case you have a hasMany (translated to: You have many ), this function returns a array of Event .

  

to summarize one returns a object and the other a array of objects

So far the explanation and the truth is all very well but ...

Now the SOLUTION

Assuming that $event is an object then $event->dependences returns an object, it can not be traversed by a foreach, so what should be done is:

$event->dependences->name;//para acceder al nombre de dependencia

In another case with $dependence->events returns an array and there you can use an array:

@foreach($dependence->events as $event)
    {{$event->name}}
@endforeach

That's the solution, but ...

Now a advice :

when you perform:

$events = Event::orderBy('id', 'DESC')->where('status', 'PUBLISHED')->paginate(3);

YOU WILL NOT CLARIFY what relationships you bring with him, which is more or less regular , since laravel being so intuitive and efficient takes all with him. p>

  

but also there you could file your error !!!

I always advise you to use with , this function allows us to decide which relationships we will extract in the query.

$events = Event::with('dependences')->orderBy('id', 'DESC')->where('status', 'PUBLISHED')->paginate(3);

Having $events the following should not fail

@foreach($events as $event)
    {{$event->name}}//nombre del event
    <br>
    {{$event->dependences->name}}//nombre de dependence del event
@endforeach 
    
answered by 17.09.2018 / 14:59
source
1

I do not know if you have tried it, but I think that in the view, you need to be a function, that is, it should be the foreach:

@foreach($event->dependences() as $dependence)
    
answered by 17.09.2018 в 12:14