K
K
kirillsnovikov2018-05-15 14:18:10
Laravel
kirillsnovikov, 2018-05-15 14:18:10

How to link two columns of the same table through one Eloquent method?

Hello!
tell me how to correctly link two columns of one table with one column of another table?
task: in the admin panel, you need to display the names of users who created the post and edited the post, how to implement this correctly? or just create two separate relationships to the users table?
set several fields as the second argument in the array in one connection does not work

public function user()
  {
    return $this->belongsTo('App\User', ['created_by', 'modified_by']);
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2018-05-15
@Result007

Hey!
Why not make 2 methods for each field? Easier to understand.

public function userCreated()
{
    return $this->belongsTo('App\User', 'created_by');
}

public function userModified()
{
    return $this->belongsTo('App\User', 'modified_by');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question