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

Pauliuso's avatar

How to get name and surname by foreign key and add it as new value to current query

Hi, I know that title is not very well described, here's what I'm trying to do, I have quite common eloquent call:

$artworks = Artwork::orderBy("created_at", "asc")->paginate(12);

artwork has id, title, year, price, and 'author_id' columns, 'author_id' is foreign_key to table with author names, surnames etc. now with above query I get info about artwork but I also want to get author name and surname as a authornamesurname value within the same query, I know how to do it in sql but I can't find any way how to do it in Eloquent.

Any help is very appreciated!

0 likes
6 replies
Cronix's avatar
Cronix
Best Answer
Level 67

This is what laravel relationships is for. You'd create an author relationship in your Artwork model.

Then you could just

$artworks = Artwork::with('author')->orderBy("created_at", "asc")->paginate(12);

which would load the author for each Artwork (which uses the author_id to retrieve behind the scenes)

Then when looping over the artworks, you can access the author properties for each one.

foreach($artworks as $artwork) {
    echo $artwork->someProperty;  // echo a field from $artwork
    echo $artwork->author->name;  // echo the authors name for this $artwork
    echo $artwork->author->surname;
}

https://laravel.com/docs/5.5/eloquent-relationships

There are quite a few videos that illustrate this. Check out the 3rd one in this series: https://laracasts.com/series/digging-in

1 like
Pauliuso's avatar

Thanks a lot! this was what I needed, however I have one more quick question, now I have author relationship in artwork model:

    public function getAuthor()
    {
        return $this->hasOne("App\User", "id", "author_id");
    }

But this selects all columns from author table, would it be possible here to select only name and surname?

Pauliuso's avatar

But in my case there are two tables users and artworks already joined, this query gets all artworks and with each artwork associates author from users table

    $artworks = Artwork::with("getAuthor")->orderBy("created_at", "asc")->paginate(12);

    public function getAuthor()
    {
        return $this->hasOne("App\User", "id", "author_id");
    }

If I would try to constrain like this

    $artworks = Artwork::with("getAuthor")->select("surname", "name")->orderBy("created_at", "asc")->paginate(12);

It would constrain based on artworks table but I need to constrain only users table.

Cronix's avatar

Yes, check the docs I linked to. Its different than what you wrote.

$artworks = Artwork::with(["getAuthor" => function($query) {
    $query->select("surname", "name");  //this will select `name`, `surname` for author only
}])->orderBy("created_at", "asc")->paginate(12);
1 like
Pauliuso's avatar

Thank you very much! I tried searching for select constraint in your link but it wasn't there, anyways now I see how to add constraints:)

I also found a funny thing, when I used select in this case I had to select id also, for example if I use select() I get everything, if I use select("surname", "name") I get null and if I use select("id", "surname", "name") it behaves as expected, so the final query would look like this:

$artworks = Artwork::with(["getAuthor" => function($query) {
    $query->select("id", "surname", "name");  //this will select `name`, `surname` for author only
}])->orderBy("created_at", "asc")->paginate(12);

Thanks!

Please or to participate in this conversation.