Laravel:使用 eloquent 从多个相关表中返回结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17964727/
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 08:12:20  来源:igfitidea点击:

Laravel: returning results from multiple related tables using eloquent

phplaravellaravel-4eloquent

提问by Josh

I'm using Laravel 4 and in particular I'm looking for an answer that uses eloquent ORM.

我正在使用 Laravel 4,特别是我正在寻找一个使用 eloquent ORM 的答案。

I have a table "tasks" which containers a client_idand a user_idassigned to each row.

我有一个表“任务”,其中包含分配给每一行的client_iduser_id

client_idrefers to a client on a "clients" table and user_idrefers to a user on a "users" table.

client_id指的是“ clients”表中的客户端,而user_id指的是“ users”表中的用户

What I want to do: show all tasks and display the "clients" nameand "users" first_name

我想要做的:显示所有任务并显示“客户的名字和“用户FIRST_NAME

So the result would look like this in my (blade) view:

所以结果在我的(刀片)视图中看起来像这样:

@foreach($tasks as $task)
    <tr>
        <td>{{ $task->user->first_name }}</td>
        <td>{{ $task->client->name }}</td>
        <td>{{ $task->description }}</td>
    </tr>
@endforeach

The above view spits out the $task->client->name perfectly fine but unfortunately shows a "Trying to get property of non-object" when I add the line $task->user->first_name

上面的视图吐出 $task->client->name 非常好,但不幸的是当我添加行 $task->user->first_name 时显示“试图获取非对象的属性”

My controller looks like this:

我的控制器看起来像这样:

$tasks = Task::with(array('user', 'client'))->get();
return View::make('index', compact('tasks'));

As I understand it my models make a difference too, so my models look like this:

据我了解,我的模型也有所作为,所以我的模型如下所示:

class Task extends Eloquent {
    protected $guarded = array();
    public static $rules = array();

    public function client() {
        return $this->belongsTo('Client');
    }

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

And:

和:

class User extends Eloquent implements UserInterface, RemindableInterface {
    public function task()
    {
        return $this->hasMany('Task');
    }
}

And:

和:

class Client extends Eloquent {
    public function projects(){
        return $this->hasMany('Project', 'client_id');
    }
}

Any ideas on how to make this work?I've been scratching my head for a while - also note I'm not a database relationship pro so the simpler the explanation the better :)

关于如何使这项工作的任何想法?我一直在挠头一段时间 - 另请注意,我不是数据库关系专家,所以解释越简单越好:)

采纳答案by user1669496

I just worked through this and learned quite a few things myself. What I did was setup a many to many relationship between usersand clientsand created a pivot table for handling the relationship called taskswhich also stores the description for each task.

我刚刚解决了这个问题,自己也学到了很多东西。我所做的是在users和之间设置多对多关系,clients并创建了一个数据透视表来处理调用的关系tasks,它还存储了每个任务的描述。

It was too much to type here, but you can check out my code at http://paste.laravel.com/Fpv

在这里输入太多了,但您可以在http://paste.laravel.com/Fpv查看我的代码

回答by ciruvan

Many-to-many relationships can be done like this with Eloquent:

使用 Eloquent 可以像这样实现多对多关系:

class User extends Eloquent implements UserInterface, RemindableInterface {
    public function client()
    {
        return $this->belongsToMany('Client', 'tasks', 'client_id')->withPivot('description');
    }
}

and the inverse relationship...

和相反的关系......

class Client extends Eloquent {
    public function users()
    {
        return $this->belongsToMany('User', 'tasks', 'user_id');
    }
}

Haven't tested this, but it should be correct.

没有测试过这个,但它应该是正确的。