Casper
Casper

Reputation: 592

Get values from belongsTo() Laravel 5

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');
    }

Upvotes: 0

Views: 11522

Answers (3)

Amirouche
Amirouche

Reputation: 3766

when you use belongsTo Laravel will assume the forgein key is 'table_id' and when use haseOne or hasMany Laravel will assume it 'tables_id' with s then to specify your own chose you need to do this

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

Upvotes: 0

The Alpha
The Alpha

Reputation: 146269

You suppose to have a user_id field in your tasks table where user_id should refer to the id column in the users table, for example:

// App\Task.php
public function userInfo()
{
    // Laravel'll assume 'user_id' (fk) is available in tasks table
    return $this->belongsTo('App\User');
}

Alternatively, you may explicitly tell the local key name using:

// App\Task.php
public function userInfo()
{
    return $this->belongsTo('App\User', 'the_key_that_refers_to_users_table');
}

Upvotes: 3

Joshua David
Joshua David

Reputation: 291

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.

Upvotes: 2

Related Questions