Declare Header
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=theincircle_csv.csv",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
Declare Columns
$columns = array(
'Name',
'Mobile',
'Email',
'Gender',
'City',
'Location',
'Skills',
'Salary',
'Experience'
);
Get data
$users = User::where('user.status',1)
->select(
'users.name',
'users.contact',
'users.email',
'users.gender',
'users.salary',
'users.exp'
)
->limit(5
)->get();
Set Formatted data to a variable
$callback = function() use ($users, $columns, $skill)
{
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach($users as $user) {
fputcsv($file, array(
$user->name,
$user->contact,
$user->email,
$user->gender,
$user->city_name,
$user->location_name,
$user->skill,
$user->salary,
$user->exp
));
}
fclose($file);
};
Return response
return Response::stream($callback, 200, $headers);
Your code should look like this
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=theincircle_csv.csv",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$users = User::where('user.status',1)
->select(
'users.name',
'users.contact',
'users.email',
'users.gender',
'users.salary',
'users.exp'
)
->limit(5
)->get();
$columns = array(
'Name',
'Mobile',
'Email',
'Gender',
'City',
'Location',
'Skills',
'Salary',
'Experience'
);
$callback = function() use ($users, $columns, $skill)
{
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach($users as $user) {
fputcsv($file, array(
$user->name,
$user->contact,
$user->email,
$user->gender,
$user->city_name,
$user->location_name,
$user->skill,
$user->salary,
$user->exp
));
}
fclose($file);
};
return Response::stream($callback, 200, $headers);
Great Post, I really appreciate to blogger for this useful information. Keep sharing.
Thanks