Laravel Relationships / attributes to common linked table

140 views Asked by At

Let me just start off by saying I'm VERY new to Laravel. I have a table for listbox items (truncated migration code below):

$table->increments('id');
$table->string('list_item');
$table->string('group');
$table->integer('sort_order')->default(1);

Group is used for e.g "TITLE", "SPECIALITIES" etc. I'm linking user_title_li_id (in the users table) to the id of this table, as well as provider_title_li_id, provider_speciality_li_id in the providers table etc

How do I set up the relationship in the User and List Item models, in order to get the list_item (e.g. Professor) for each Model in the controller?

1

There are 1 answers

0
CASH On

In your User model, define a function like this:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the list items for the user.
     */
    public function listItems()
    {
        return $this->hasMany('App\ListItem', 'foreign_key', 'list_item');
    }
}