Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimeSocks's avatar

How can I retrieve this database field?

I'm going slightly mad.

I have an Event model and an Instance model. An Event hasMany Instances and each Instance belongsTo one Event.

In Eloquent I can therefore say:

Event::find(1)->instances

and list all the instances of an Event.

How can I do the reverse and get the Event information for a specific Instance? If I say:

Instance::find(1)->events

It just returns null

Models:

class Event extends Model {

    public function instances()
    {

        return $this->hasMany('App\Instance');

    }

    public function categories()
    {
        return $this->belongsToMany('App\Category');
    }


}
class Instance extends Model {

    protected $fillable = [

        'event_id',
        'instance_date',
        'venue',

    ];

    protected $dates = [

        'instance_date'

    ];

    public function scopeMatchEventId($query,$id)
    {

        return $query->where('event_id', '=', $id);

    }

    public function events()
    {

        return $this->belongsTo('App\Event');

    }

    public function venues()
    {
        return $this->belongsToMany('App\Venue');
    }
}
0 likes
34 replies
mstnorris's avatar

Show us the code in your models.

This is just a guess, but you might have issues with the names of your models.

Event and Instance are terms used widely through Laravel and PHP in general. Even if this isn't an issue for PHP to handle, I would suggest renaming them to something a little clearer.

mstnorris's avatar

@TimeSocks does the Instance with an ID of 1 actually belong to any Events?

Try something like this:

Event::find(1)->instances;

Use one of the IDs that was returned as the $id for the code below:

Instance::find($id)->events;
TimeSocks's avatar

@mstnorris yes, if I run App\Event::find(1)->instances in Tinker I get a list of instances. I've cherry-picked several of those listed to do in reverse. No dice.

mstnorris's avatar

Just to please me, rename your methods to something other than events() to see if there is a collision somewhere.

TimeSocks's avatar

Already been trying it since you mentioned it... wibbles are just as ineffective as events I'm afraid.

pmall's avatar
pmall
Best Answer
Level 56

As there is only one parent event, name your method event. Without s.

The foreign key is inferred from the relation name. It should work with event. Otherwise you can specify it with the second param.

2 likes
mstnorris's avatar

@pmall nice catch ;)

Laravel makes this stuff so simple, I forget sometimes that you need to bend to its will to do these types of things.

TimeSocks's avatar

@pmall a-ha, that's got it... in Tinker at least.

Supplementary question for super-bonus points: how would I do a join so that I can retrieve all the Instance information for a given instance, but also the event_name from its associated Event?

p.s. @mstnorris thanks for your input too.

pmall's avatar

Laravel makes this stuff so simple, I forget sometimes that you need to bend to its will to do these types of things.

always stick to the convention.

Plus it allow things like parent() relationship that automagically guess parent_id.

1 like
pmall's avatar

Supplementary question for super-bonus points: how would I do a join so that I can retrieve all the Instance information for a given instance, but also the event_name from its associated Event?

You dont do a join, you eager load the events. $instances = Instance::with('event');.

1 like
TimeSocks's avatar

@pmall Aha, of course. I'm an idiot.

Erm, one more thing... how do I retrieve just one column from event as opposed to all columns?

martinbean's avatar

@TimeSocks If an instance only belongs to one event, then rename the relation to just be event, i.e.

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

It could be that the name “events” conflicts with some thing internal to Laravel.

pmall's avatar

Erm, one more thing... how do I retrieve just one column from event as opposed to all columns?

Why do you care about this ?

mstnorris's avatar

@TimeSocks you have to query the events table anyway so it doesn't matter, just use the above and then, once you have the $event use $event->name

TimeSocks's avatar

@pmall because it seems messy to get all the event information (which includes a lot of text in many cases) when all I need is the event_name. Maybe it doesn't matter ¯_(ツ)_/¯

TimeSocks's avatar

@mstnorris Okay. So I'm passing my nicely retrieved $instance variable to my view. Now how do I access the information in the event section in my view? I've tried $instance->event->event_name and it threw an error.

mstnorris's avatar

What error, please give details as it might be unrelated. This should also be a new question.

Anyway, is event_name the name of the field?

TimeSocks's avatar

@mstnorris Yes. I've just done it actually, by putting it in a foreach loop:

@foreach($instance as $event)
    {{ $event->event->event_name }}
@endforeach

Seems somehow wasteful to go through a loop for something that will always be one item though. Is there a better way?

mstnorris's avatar

How are you passing $instance to the view? Can I see that logic.

mstnorris's avatar

@TimeSocks what is $instance? Show me that code too.

You probably need to use

$instance = Instance::with('event')->whereId($id)->first();

return view('instance.show', compact('instance'));

And then in your view you can do as you were before.

{{ $instance->event->event_name }}
Next

Please or to participate in this conversation.