Reputation: 157
I'm learning Laravel right now and I'm stumped on how to get an array of records from one table that belong to a record on another table based on a key.
I have two tables:
titles
-------------------
id | title_name | created_at | updated_at
posts
-------------------
id | titles_id | content
I have the route /{title_name} being controlled by the read() method on my PagesController.php
public function read($title){
$title_name = $title;
$title_id = Title::find($title)->id;
$posts = Title::find($title)->posts;
return view('pages/read')->with([
'title_name' => $title_name,
'title_id' => $title_id,
'posts' => $posts
]);
}
But this doesn't seem to output anything. I have my models setup like this:
Title.php
class Title extends Model
{
// Table Name
protected $table = "titles";
// Primary Key
protected $primaryKey = "title";
// Timestamps
public $timestamps = "true";
// Custom primaryKey
public $incrementing = false;
//relationship
public function posts(){
return $this->hasMany('App\Post', 'titles_id')->orderBy('created_at', 'desc');
}
}
Post.php
class Post extends Model
{
// Table Name
protected $table = "posts";
// Primary Key
protected $primaryKey = "id";
// Timestamps
public $timestamps = "true";
//relationship
public function titles(){
return $this->belongsTo('App\Title');
}
}
I think the problem is that when I do Title::find($title)->post, laravel is trying to find posts where the titles_id = title_name, because I set title_name as the primaryKey, but I need it to be looking for the id column in the Titles table, and not the name...
Upvotes: 0
Views: 1984
Reputation: 3497
Alright I will give you an example where I explain everything you do wrong.
Tables:
titles
-------------------
id | title_name | created_at | updated_at
posts
-------------------
id | title_id | content
Not titles_id
but title_id
, eloquent likes this more.
Your controller:
public function read($titleName){
// The first function argument is the name of the title,
// not the title model.
// Also don't use snake_case in laravel(Except helpers) but camelCase.
// We are not going to use find, you might have set the name as
// primary key, but the id column still exists.
// firstOrFail() means get the first result, if there isn't, throw
// a model not found exception(404).
$title = Title::where('name', $titleName)->firstOrFail();
return view('pages/read')->with([
// You could just do 'title' => $title, and do the rest in the view.
'title_name' => $title->name,
'title_id' => $title->id,
'posts' => $title->posts
]);
}
Title model:
class Title extends Model
{
// $table not needed, laravel knows this(Yes pure magic).
// No, we don't want name as primary key.
// Timestamps is true by default, so we don't need it.
public function posts(){
return $this->hasMany(\App\Post::class)->orderBy('created_at', 'desc');
}
}
Post model:
class Post extends Model
{
// This function should be called title, not titles.
public function title(){
return $this->belongsTo(App\Title::class);
}
}
Upvotes: 1