从belongsTo() Laravel 5 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34572587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:57:25  来源:igfitidea点击:

Get values from belongsTo() Laravel 5

laravellaravel-5eloquent

提问by Casper

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

我在使用 Laravel 5 和 Eloquent 时遇到了一些问题。我有 3 个模型 userInfo、taskAssign 和 taskCreated。用户模型:

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?

我应该能够从我的用户那里获取值(URL 中的 WHERE id = id)以显示在我的任务显示视图中的belongsTo() 中吗?像这样:{{$tasks->userInfo->name}}还是我完全不在这儿?



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:

和我的观点{{$tasks->assignee}},但它没有显示任何内容,我也在尝试获取受让人的信息,但感谢所有示例,它们稍后会很有用。和模型:

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

回答by The Alpha

You suppose to have a user_idfield in your taskstable where user_idshould refer to the idcolumn in the userstable, for example:

你假设user_id你的tasks表中有一个字段user_id应该引用表中的idusers,例如:

// 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 nameusing:

或者,您可以明确告诉local key name使用:

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

回答by Joshua David

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.

在英语中,一个任务有一个创建者和一个接收者。我不确定您要通过->userInfo()关系访问哪个,所以我将描述访问方式。

Because you're not using the name user_idas the foreign key on the tasktable, you'll need to specify which foreign key you want to load users from.

因为您没有使用名称user_id作为task表上的外键,所以您需要指定要从中加载用户的外键。

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 Usermodel and the child model is the Taskmodel.

如果父模型具有->hasOne()->hasMany()关系,则子模型必须具有互补->belongsTo()关系。在这种情况下,父模型是User模型,子模型是Task模型。

回答by Amirouche Zeggagh

when you use belongsToLaravel will assume the forgein key is 'table_id' and when use haseOneor hasManyLaravel will assume it 'tables_id' with sthen to specify your own chose you need to do this

当你使用belongsToLaravel 时会假设 forgein 键是“table_id”,当使用haseOnehasManyLaravel 会假设它是“tables_id”s然后指定你自己的选择你需要这样做

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