Ad

Get Values From BelongsTo() Laravel 5

- 1 answer

i'm having a bit of a problem with Laravel 5 and Eloquent. I have 3 models userInfo, tasksAssign, and taskCreated. The User model:

public function tasksAssign()
{
    return $this->hasMany('App\Tasks', 'fk_user_id_assign', 'id')->where('status',  2);
}
public function tasksCreated()
{
    return $this->hasMany('App\Tasks', 'fk_user_id_created', 'id');
}

The tasks model

public function userInfo()
{
    return $this->belongsTo('App\User');
    //return $this->hasOne('App\User', 'id', 'id'); The hasOne here is just me testing diffrent stuff to see if i could get it to work.
}

Shoulndt i be able to get the value from my User (WHERE id = id in the URL) to show up in my tasks show view with the belongsTo()? like so: {{$tasks->userInfo->name}} or am i totally off here?


Update: Current Taskscontroller:

public function show($id)
    {
        $tasks = Tasks::findOrFail($id);
        $assignee = $tasks->assignee;
        return view('tasks.show')->withTasks($tasks);
    }

and my view {{$tasks->assignee}}, but its not showing anything, also i am trying to get the info of the assignee, but thanks for all the examples, they will be usefull later on. and the Model:

public function assignee() {
        return $this->belongsTo('App\User', 'fk_user_assign_id', 'id');
    }
Ad

Answer

Okay, so I'm assuming your schema looks like this:

Schema::create('user', function($table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('task', function($table) {
    $table->increments('id');
    $table->integer('fk_user_id_assign')->unsigned();
    $table->foreign('fk_user_id_assign')->references('id')->on('user');
    $table->integer('fk_user_id_created')->unsigned();
    $table->foreign('fk_user_id_created')->references('id')->on('user')
    $table->timestamps();
});

In English, a task has a creator, and has an asignee. I'm not sure which you're trying to access with the ->userInfo() relation, so I'll describe the way to access either.

Because you're not using the name user_id as the foreign key on the task table, you'll need to specify which foreign key you want to load users from.

class User extends Eloquent {
    public function createdTasks() {
        return $this->hasMany(Task::class, 'fk_user_created_id', 'id');
    }

    public function assignedTasks() {
        return $this->hasMany(Task::class, 'fk_user_assign_id', 'id');
    }
}

class Task extends Eloquent {
    public function creator() {
        return $this->belongsTo(User::class, 'fk_user_created_id', 'id');
    }

    public function assignee() {
        return $this->belongsTo(User::class, 'fk_user_assign_id', 'id');
    }
}

To get all tasks created by a given user, use

$tasksCreatedByUser = $user->createdTasks;

To get all tasks assigned to a given user, use

$tasksAssignedToUser = $user->assignedTasks;

To get the user a task was created by, use

$creator = $task->creator;

To get the user a task is assigned to, use

$assignee = $task->assignee;

Finally, if you want to do something like list the assignee of every task created by a user, do something like

$tasksCreatedByUser = $user->createdTasks;
foreach($tasksCreatedByUser as $task) {
    $assignee = $task->assignee;
    echo "Task #{$task->id} was created by user #{$user->id} and assigned to user #{$assignee->id}\n";
}

If the parent model has a ->hasOne() or ->hasMany() relation, then the child model must have a complementary ->belongsTo() relation. In this case, the parent model is the User model and the child model is the Task model.

Ad
source: stackoverflow.com
Ad