T
T
Troodi Larson2019-03-06 14:14:28
Laravel
Troodi Larson, 2019-03-06 14:14:28

BelongsTo takes only the id of the post, why?

Good afternoon. I ran into a problem that I don't quite understand.
There is a standard table of users.
There is a table of tasks where the user id is in the "user" field.

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

This code is in the task model.
$task = TaskHistory::where('id', $id)->firstOrFail();
$user = $task->user->name;

And this code is trying to get the username. But I get the error "trying to get property"
If it's simple to call $task->user, it will display the ID. I tried to change a lot of things, according to the advice in Google, but I never came to the right option. Where did I go wrong?
Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-03-07
@troodi

There is a table of tasks where the user id is in the "user" field.

Well, what is the question? :)
//name the field user_id and don't fool yourself))) or rename the function (is it possible?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question