$story = Story::with('author')->where('slug', $slug)->first();
$name = $story->author->name;
I'm assuming you only want one article since you're using slug to find it..
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to get an author's name to be displayed in a view. I have 3 tables: authors, author_story and story.
I need to get the author's name by going through the story table.
Story model
public function author(){
return $this->belongsToMany('App\Modules\Authors\Models\Author', 'author_story', 'story_id', 'author_id');
}
My author model
public function story(){
return $this->belongsToMany('App\Modules\Authors\Models\Story', 'author_story', 'author_id', 'story_id');
}
My controller
public function slug($slug){
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$stories = Story::where('slug', $slug)->get();
$test = Story::___->author()->get();
$name = $test->name;
return view('open::public.single-story', compact('menus_child', 'stories'));
}
The section that just has "___" is the section where I'm stuck. I'm not sure on what needs to go there.
Please or to participate in this conversation.