laravelquery-builderexport-csvcustomcolumn

Answer: Laravel 7, select dynamic columns in query builder


I have spent a lot of my time to find this solution. I want to export a user selected list of columns data and here is the solution for this. The solution is getting a list of columns in array form and then I am converting it into a specific database column names and then passing it to the query. The query builder of laravel accepts the array. This made my life easy and I want to make your life easy :)

Ask me if anything is not clear.

 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);
}

Solution

  • You can use addSelect function to dynamic at the column in system programming way.

    Based on your solution replace following lines:-

    $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();   
    

    into

    $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();