【问题标题】:laravel excel getting only current datalaravel excel 只获取当前数据
【发布时间】:2017-12-04 13:02:50
【问题描述】:

我刚开始使用这个 excel 包 maatwebsite/excel,我设法下载了 excel。但是在创建excel时我遇到了一些困难

  1. 如何更改 Excel 中打印的日期格式?我看到有人使用这样的东西,但为什么我的不能工作?

$sheet->setColumnFormat(array('E' => 'd/m/y'));

  1. 如何获取今天的数据?例如,我输入了一些数据并保存到数据库中,当我下载 excel 时,它只会显示该数据而不是过去的数据。我该怎么做?

  2. 可以更改excel的标题吗?例如 created_at 更改为 Date

目前这里是我的代码:

 public function downloadExcel($type)
    {

        $data = personal_info::join('qualifications', 'qualifications.user_id', '=', 'personal_infos.id')
        ->select(
          'personal_infos.id',  
          'personal_infos.name', 
          'personal_infos.email', 
          'qualifications.qualification',
          'personal_infos.created_at')
        ->get()
        ->sortByDesc('created_at');
        return Excel::create('test', function($excel) use ($data) {
            $excel->sheet('mySheet', function($sheet) use ($data)
            {
                $sheet->fromArray($data);
                $sheet->setColumnFormat(array('E' => 'd/m/y'));
            });
        })->download($type);
    }

【问题讨论】:

  • 对于您的所有问题,您可以在视图中执行此操作,对吗?然后将该视图导出到 excel,@Blade 到 Excel (maatwebsite.nl/laravel-excel/docs/blade)
  • 抱歉,我可以在视图中完成是什么意思?你的意思是我可以在我的视图中下载 excel 或其他东西,还是我可以在我的 excel 中看到数据?还有你给的链接,我已经读过了,我不太明白
  • 如果可能的话,你能给我举个例子,这样我就能更清楚地理解它吗? @arunkumar

标签: php excel laravel


【解决方案1】:

如果您想要今天的数据,请在查询中添加 where 条件:-

$todaydate = date('Y-m-d');
$data = personal_info::join('qualifications', 'qualifications.user_id', '=', 'personal_infos.id')
    ->select(
      'personal_infos.id',  
      'personal_infos.name', 
      'personal_infos.email', 
      'qualifications.qualification',
      'personal_infos.created_at')
    ->where('personal_infos.created_at',$todaydate) // or you can use whereDate() of laravel like :- whereDate('personal_infos.created_at', '=', date('Y-m-d'));
    ->get();

是的,可以将 created_at 更改为 Date 您需要提供这样的别名:-

enter code here
$data = personal_info::join('qualifications', 'qualifications.user_id', '=', 'personal_infos.id')
    ->select(
      'personal_infos.id',  
      'personal_infos.name', 
      'personal_infos.email', 
      'qualifications.qualification',
      'personal_infos.created_at as Date') //here you can change the name
     ->where('Date',$todaydate) // then you need to change in where condition also 
     ->get();

希望对你有帮助!

【讨论】:

  • 将 created 更改为 Date 有效,非常感谢!但是当我使用->where('Date',$todaydate)时,我收到了这个错误,'where子句'中的未知列'Date'。我尝试用 personal_infos.created_at 替换 Date ,没有错误,但不会显示任何数据。因此,当我尝试删除 where 子句时,有数据,那么这里可能是什么问题?
  • 检查今天日期的数据是否存在@Dkna
  • 是的,日期在那里,我尝试做一个 dd 并导致我这样做,#attributes: array:5 [▼ "id" => 5 "name" => "test" "email " => "hello@gmail.com" "qualification" => "adw" "Date" => "2017-11-08 05:19:43" ] 没有 where 子句
  • @Dkna today date in 2017-12-04... 我想你没有这个日期的数据
  • 哦,是的,我忘记添加数据了哈哈哈,非常感谢现在就去添加数据
猜你喜欢
  • 2018-06-10
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 2021-06-30
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
相关资源
最近更新 更多