Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shiva's avatar
Level 5

Getting a name from a different table

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.

0 likes
16 replies
SaeedPrez's avatar
    $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..

tykus's avatar

If slugs are unique (and why wouldn't they be?), then you need to fetch a Story object, not a collection:

$story = Story::where('slug', $slug)->first();

You can also eager-load the author:

$story = Story::with('author')->where('slug', $slug)->first();
$name = $story->author->name;
Shiva's avatar
Level 5

@SaeedPrez, @tykus - I get this error

Exception in Collection.php line 1498: Property [name] does not exist on this collection instance.

SaeedPrez's avatar

@Shiva you need to then check to see what your relationship is returning,..

    $story = Story::with('author')->where('slug', $slug)->first();

    // this should dump the Author model instance if setup correctly
    dd($story->author);

    $name = $story->author->name;
Shiva's avatar
Level 5

@SaeedPrez - It isn't returning anything this is what I get

Collection {#307 ▼ #items: [] }

tykus's avatar

Your relationship name author is confusing... it suggests that there is only one Author for a Story; a bgleongsToMany returns a Collection. If you definitely have at least one author for the story:

$name = $story->author[0]->name; // first author name
SaeedPrez's avatar

@Shiva As you can see, you get a collection back which can contain multiple objects (read multiple authors).

You have a couple of options, if you only want to get the first author's name, you can do:

$story->author->first()->name;

If you want to get all authors' names, you can do (i.e. in your view)..

@foreach($story->author as $author)
    <li>{{ $author->name }}</li>
@endforeach

Edit: Like tykus suggested, it might be wise to rename your method from author() to authors() since it's a many to many relationship.

Shiva's avatar
Level 5

@SaeedPrez - I'm now getting this error

ErrorException in OpenController.php line 64: Trying to get property of non-object

Line 64 is

$name = $story->author->first()->name;
SaeedPrez's avatar

@Shiva

Instead of this..

    dd($story->author);

Try this..

    dd($story->author->toArray());

..and post the results here. Maybe your relationship query doesn't return anything?

SaeedPrez's avatar

@Shiva do you have any data in your database tables to connect the story you're querying to authors?

SaeedPrez's avatar

Ok, then let's take a step back.. can a story have multiple authors or can a story only have one author?

Shiva's avatar
Level 5

@SaeedPrez - I am so sorry. I double checked my database and the story I was using didn't have an author. Thank you guys for helping me.

SaeedPrez's avatar

@Shiva Glad I could help ☺ Don't forget to select best answer to mark this topic as solved and to help others quickly find the correct answer.

user224564's avatar

{{\App\Author::find($story->author_name)->name}}

Author-> Name of the model where you have authors

($story->author_name)-> The columm where you have author id or name in story tabel

name-> columm in Author table where you have the name.

Please or to participate in this conversation.