Custom Key Name with field from different table

Posted 1 year ago by Tipoff

I have two database tables that I want to use in my model with the main one being a subset of authors, those with published books. The model should only include authors that are in this table but I want to use a custom Key Name for the model of the slug that is associated with the author in the larger authors table. I can't find anything on how to accomplish this and would appreciate your help. Here's what I have so far in App/BookAuthor:

class BookAuthor extends Model
    public $table = "book_authors";
    public $table = "authors";

     * Get the route key for the model.
     * @return string
    public function getRouteKeyName()
        return 'slug'; //This field is only in the authors table

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.