Can you give in detail the internal structures of your tables?
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.
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
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
Looks like you have your models reversed:
Show belongs to Show Title Show Title has one Show
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');
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');
}
}
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'?
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.
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.
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?
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.
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');
}
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.
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.
Use this in your model:
protected $table = 'table_name';
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community