【发布时间】:2018-04-06 18:56:57
【问题描述】:
我是 Laravel 的新手, 理解查询有一些困难
我有两张桌子
用户 [id, email, fname, lname]
Leads [id, other some fields, client, agent]
client和agent是users表id的外键..
使用下面的代码,我可以从 Leads 表中检索数据 但我得到了客户= 3 agent = 4 这样的东西
但我不想显示这些 id,而是想从用户表中获取这些 id 的名称
怎么做?
在控制器中我现在有这个功能
/**
* @return \Illuminate\Http\Response
*/
public function leads(){
if(Auth::user()->role == 'admin'){
$leads = Leads::all();
// here in the $leads i get datas like ['id' => 1, 'field1' => 'test', 'field2'=> 'test2', 'client_id'=> 2, 'agent_id'=> 3] , but i want to get the names of the client and agent from the user table which we can do in normal sql query by joining but how to do with laravel?
}
return view('leads', compact('leads'));
}
怎么做?
在用户模型和潜在客户模型中,我只有默认代码我有代码
如果能得到一些快速帮助将不胜感激
【问题讨论】:
-
你的要求真的很混乱。据我所知,你是想从经纪人那里看到吗?
-
嗨,感谢您的快速回复,让我澄清一下。当创建一个新的潜在客户时,它在潜在客户表的代理列中具有当前代理 ID,对于我有 id 的客户也是如此,但需要检索这些客户或代理的名称,并需要在视图中显示它们
-
尝试更新问题并澄清更多。
标签: php mysql laravel orm eloquent