【问题标题】:How to print name of a user in Excel when in DB is set with its ID. PHP, Laravel如何在 DB 中使用其 ID 设置时在 Excel 中打印用户的名称。 PHP, Laravel
【发布时间】:2017-08-29 08:58:49
【问题描述】:

我从基于 Laravel 框架的网页导出表格到 excel,但是当我下载它下载的文件时。

但我想在这些文件中显示与特定 ID 对应的用户名。项目也一样。 (但如果找到用户名的解决方案,也可以对项目使用相同的解决方案。

【问题讨论】:

  • 您只希望在 Excel 工作表中显示名称和 ID??
  • @KuldeepMishra 我想要图片中显示的所有列,但是 name_id 在哪里,我想输入用户的全名。示例:$user->fullName
  • 您需要根据 name_id 加入 eloquent 查询中包含全名的任何表。
  • 为了测试,我只注册了1个ID,但是会有不同的用户。
  • @RobFonseca 知道怎么做吗?

标签: php excel laravel


【解决方案1】:

您需要根据 Timesheets 表中的 user_id 为您的用户表创建一个联接。将您的 eloquent 查询修改为:

$data = Timesheet::select('timesheets.name', 'timesheets.description',
                          'timesheets.project_id', 'users.fullName',
                          'timesheets.hours', 'timesheets.date',
                          'timesheets.created_at', 'timesheets.updated_at')
                 ->join('users', 'timesheets.user_id', '=', 'users.id')
                 ->get()->toArray();

我还指定要提取的特定列,以便您可以完全控制电子表格中显示的数据。

我看到您也有 project_id,因此您需要在查询中添加另一个 join() 方法来处理项目表。

【讨论】:

  • 嘿,抱歉迟到了,我一直收到这个错误
  • SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'users.fullName'(SQL:选择timesheets.nametimesheets.descriptiontimesheets.project_id, users.fullName, timesheets.hours, timesheets.date, timesheets.created_at, timesheets.updated_at from @987654338 @inner join users on timesheets.user_id = users.id) 所以基本上它找不到clients.fullName。
  • 有客户表吗?我的答案有 users.fullName。如果“fullName”不存在,则需要将名字和姓氏列连接在一起
  • 对不起,看来是我的错误,我更新了评论。我的意思是 users.fullName 不是客户
  • 您的用户表中的列 fullName 吗?您尚未显示 User 模型,因此我们对表格中的列一无所知。
猜你喜欢
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 2021-06-19
  • 2015-02-10
  • 2015-10-22
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多