I have 3 tables
user: id, username..
subreddits: id, name, created_at..
posts: id, title, link, user_id, subreddit_id
The problem is, I am fetching the id of the subreddit/category manually while I need to be fetching it dynamically. How can I achieve that?
This is the show() method in SubredditController.php
public function show(Subreddit $subreddit)
{
$posts = Subreddit::findOrFail(3)->posts()->get();
return view('subreddit/show')->with('subreddit', $subreddit)
->with('posts', $posts);
}
And this is Subreddit Model
class Subreddit extends Model
{
protected $fillable = [
'name',
'description'
];
public function user() {
return $this->belongsTo('App\User');
}
public function posts() {
return $this->hasMany('App\Post');
}
}
Also. am I doing this right? Is there a better way?