【问题标题】:Answer: Laravel 7, select dynamic columns in query builder答:Laravel 7,在查询生成器中选择动态列
【发布时间】:2020-04-23 02:04:17
【问题描述】:

我花了很多时间来寻找这个解决方案。我想导出用户选择的列数据列表,这是解决方案。解决方案是以数组形式获取列列表,然后将其转换为特定的数据库列名,然后将其传递给查询。 laravel 的查询构造器接受数组。这让我的生活变得轻松,我想让你的生活变得轻松:)

如果有什么不清楚的地方可以问我。

 public function ExportData(Request $request)
    {
      $name = 'ArchiveData'.time();
      $headers = [
         'Cache-Control'       => 'must-revalidate, post-check=0, pre-check=0'
         ,   'Content-type'        => 'text/csv'
         ,   'Content-Disposition' => 'attachment; filename='.$name.'.csv'
         ,   'Expires'             => '0'
         ,   'Pragma'              => 'public'
     ];

     $column_title = array('a.a_sg' => 'Signature','a.a_sgo' => 'Signature old','p.p_title' => 'Portfolio','s.s_title' => 'Series','b.b_title' => 'Bundle','t.t_title' => 'Type','a.a_description' => 'Description','a.a_date_from' => 'Date From','a.a_date_to' => 'Date To','l.l_title' => 'Location','pe.pe_title' => 'Person','a.a_pr_note' => 'Private Note','ta.ta_title' => 'Tag');
     $ex_columns = array();
     foreach($column_title as $key => $value) {
      if(in_array($value,$request->exc)){

        $ex_columns[] = $key;
    }
}

$data =  DB::table('archive_data as a')
->join('person as pe', 'pe.pe_id', '=', 'a.a_pe_id')
->join('portfolio as p', 'p.p_id', '=', 'a.a_p_id')
->join('tag as ta', 'ta.ta_id', '=', 'a.a_ta_id')
->join('type as t', 't.t_id', '=', 'a.a_ty_id')
->join('location as l', 'l.l_id', '=', 'a.a_lc_id')
->join('series as s', 's.s_id', '=', 'a.a_s_id')
->join('bundle as b', 'b.b_id', '=', 'a.a_b_id')
->whereIn('a.a_id', $request->bk)
->select($ex_columns)
->get();   

                    // dd(DB::getQueryLog());

$data = json_decode(json_encode($data), true);

array_unshift($data, $request->exc);



$callback = function() use ($data) 
{
 $FH = fopen('php://output', 'w');
 foreach ($data as $row) { 
     fputcsv($FH, $row);
 }
 fclose($FH);
};

return Response::stream($callback, 200, $headers);
}

【问题讨论】:

  • 那么你的问题是什么?想要增强或此代码导致错误?
  • 我相信这不是一个问题,而是一个 CSV 助手。

标签: laravel query-builder export-csv customcolumn


【解决方案1】:

您可以使用addSelect函数以系统编程的方式在列处动态化。

根据您的解决方案替换以下行:-

$data =  DB::table('archive_data as a')
->join('person as pe', 'pe.pe_id', '=', 'a.a_pe_id')
->join('portfolio as p', 'p.p_id', '=', 'a.a_p_id')
->join('tag as ta', 'ta.ta_id', '=', 'a.a_ta_id')
->join('type as t', 't.t_id', '=', 'a.a_ty_id')
->join('location as l', 'l.l_id', '=', 'a.a_lc_id')
->join('series as s', 's.s_id', '=', 'a.a_s_id')
->join('bundle as b', 'b.b_id', '=', 'a.a_b_id')
->whereIn('a.a_id', $request->bk)
->select($ex_columns)
->get();   

进入

$dataQuery =  DB::table('archive_data as a')
->join('person as pe', 'pe.pe_id', '=', 'a.a_pe_id')
->join('portfolio as p', 'p.p_id', '=', 'a.a_p_id')
->join('tag as ta', 'ta.ta_id', '=', 'a.a_ta_id')
->join('type as t', 't.t_id', '=', 'a.a_ty_id')
->join('location as l', 'l.l_id', '=', 'a.a_lc_id')
->join('series as s', 's.s_id', '=', 'a.a_s_id')
->join('bundle as b', 'b.b_id', '=', 'a.a_b_id')
->whereIn('a.a_id', $request->bk);   

foreach($ex_columns as $ex_c){
$dataQuery = $dataQuery->addSelect($ex_c);
}

$data =  $dataQuery->get();

【讨论】:

  • 谢谢,addSelect() 工作正常。我想知道为什么这些解决方案在文档中不可用。请向我推荐此类解决方案的任何来源。感谢您提供的出色解决方案。
猜你喜欢
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
相关资源
最近更新 更多