Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Can you give in detail the internal structures of your tables?

Last updated 2 years ago.
0

Definitely.

shows:
id
title_id

shows_titles:
id
display_title

title_id relates to id. Each show can have multiple titles. But the relationship above is to show the title listed in title_id.

Last updated 2 years ago.
0

Also, try dumping the SQL that was generated during the request. You can do that like so:

dd(DB::getQueryLog());

Or install this package, along with a chrome extension, to get request details and logging in your Chrome dev tools panel: https://github.com/itsgoingd/clockwork

Last updated 2 years ago.
0

I think you should change your view to

<ul class="no-bullet">
      @foreach($shows as $show)

            <li><a href="{{ url('shows/'.$show->id) }}">{{ $show->title->display_title }}</a></li>

      @endforeach
</ul>

Notice show vs shows

Last updated 2 years ago.
0

Looks like you have your models reversed:

Show belongs to Show Title Show Title has one Show

Last updated 2 years ago.
0

According to documentation example for belongsTo relation: return $this->belongsTo('User', 'local_key', 'parent_key');

so i guess you have to change the line in you title model from return $this->belongsTo('Show', 'title_id', 'id'); to return $this->belongsTo('Show', 'id', 'title_id');

Last updated 2 years ago.
0

Both returns have to be 'Show'?

Last updated 2 years ago.
0

jerauf said:

Both returns have to be 'Show'?

class Show extends Eloquent
{
      public function title()
      {
          return $this->belongsTo('Title', 'title_id');
      }
}

class Title extends Eloquent
{
      protected $table = 'shows_titles';

      public function shows()
      {
          return $this->hasOne('Show', 'title_id');
      }
}
Last updated 2 years ago.
0

I'm getting an 'undefined property' error when using this.

Why is title() belongsTo and shows() hasOne? As I'm learning this, this is really confusing me.

Also, why don't I need 'id' in the returns? Doesn't that establish which columns are related to which? Or by default, is everything related to 'id'?

Last updated 2 years ago.
0

Any ideas as to why I'm getting an 'undefined property' errors when using crhayes' solution above?

Also, my other relationship questions in my last post. For some reason I'm just not understanding this.

Last updated 2 years ago.
0

jerauf said:

I'm getting an 'undefined property' error when using this.

Why is title() belongsTo and shows() hasOne? As I'm learning this, this is really confusing me.

Also, why don't I need 'id' in the returns? Doesn't that establish which columns are related to which? Or by default, is everything related to 'id'?

As from what you have said;

A show will have one title; a title -> belongsTo -> show

And a show will has one title. a show -> hasOne -> show

From what you have said these are the relationships between how the data relates to each other.

And in reply to "Both returns have to be 'Show'?", the point of the relationships it to retrieve the other data not the same data you already have.

Last updated 2 years ago.
0

That goes against what crhayes said above. Is his code wrong? Having both functions referencing the same class goes against his code and the tutorials that I've read.

Why would I be getting an 'undefined property' error from it?

Last updated 2 years ago.
0

Maybe if I post how I would write the query if I weren't using Laravel.

SELECT * FROM shows INNER JOIN shows_titles ON shows.title_id = shows_titles.id WHERE shows.id = $id

That's ultimately what I'm trying to achieve.

When I write it in the controller as:

Show::join('shows_titles', 'shows.title_id', '=', 'shows_titles.id')->where('shows.id', '=', $id)->first();

I don't get an error and the displays as it should.

But I really want to learn how use Eloquent. This is my first project using Laravel and I'm learning as I go.

Any help would be appreciated.

Last updated 2 years ago.
0

Each show can have multiple titles.

    public function title()
     {
         return $this->HasMany('Title', 'id', 'title_id');
      }

     public function shows()
     {
        return $this->belongsTo('Show', 'title_id', 'id');
     }
Last updated 2 years ago.
0

All right. I tried that. But I'm getting 'undefined property' on $shows->title->display_title. So it's not finding the display_title data in the titles table.

Then, I also tried this in the controller:

Show::with('title')->all();

That gave me an 'undefined method' error.

Last updated 2 years ago.
0

Anyone have any ideas? Thank you.

Last updated 2 years ago.
0

jerauf said:

Anyone have any ideas? Thank you.

Did you manage to solve this? I have a similar problem "Trying to get property of non-object" when one of the database tables has a different name to the model. Many thanks.

Last updated 2 years ago.
0

Use this in your model:

protected $table = 'table_name';
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jerauf jerauf Joined 16 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.