Thursday, 13 June 2019

Eloquent pivot table with different column name

Users table

- id
- name (string)

Visits table

- id
- user_id
- viewer_id 

I want to get a list of visits with the linked username for a given user (in this case id = 1)

$visits = \Visits::where(['user_id' => 1])->with('user')->get();

this is my model:

class Visits extends Model
{

    protected $table = 'visits';

    public function store(Request $request)
    {
        $visits = new Visits;
        $visits->save();
    }

    public function user()
    {
        return $this->belongsTo('User', 'viewer_id');
    }

}

it returns the data but the "user" is null



from Eloquent pivot table with different column name

No comments:

Post a Comment