I want to download the data of table in excel file
I have an application in Laravel and I want to add a functionality that download user data in .xls format
Route::get('users/download', function(){
$table = App\User::all();
$filename = "User.csv";
$handle = fopen($filename, 'w+');
fputcsv($handle, array('ID', 'User Name'));
foreach($table as $row) {
fputcsv($handle, array($row['id'], $row['username']));
}
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv'
);
return Response::download($filename, 'User.csv', $headers);
});
This code works fine for .csv files what changes are required for .xls file?
Take a look at Laravel Excel: https://github.com/Maatwebsite/Laravel-Excel
Its very easy to use with Laravel.
Related
Hiii , I have this formdata with few data and files. I want to store them into my database table.
this is my controller to store into the UserApplyJob model
public function store(Request $request)
{
$file = $request->file('resume');
Cloudder::upload($file->getRealPath(), null, ['resource_type' => 'raw']);
$publicId = Cloudder::getPublicId();
$url =Cloudder::show($publicId, ['resource_type' => 'raw']);
$requestJob = UserApplyJob::create($request->all());
}
You're not persisting it to the db. Try this at the end.
$requestJob->save();
This is from laravel 7.14
if($request->hasFile('cover_image'))
{
// Get the file with extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
//Get the file name
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get the ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
//File name to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
}
// Save the image to column
$blog->cover_image = $fileNameToStore;
i am trying to upload video on my laravel project it didn't uploading but when i am trying to upload images it will works perfectly
{
$request->validate([
'file' => 'required',
]);
$fileName = time();
$request->file->move(public_path('videos'), $fileName);
$fileupload = new FileUpload;
$fileupload->filename=$fileName;
$fileupload->save();
return response()->json(['success'=>'File Uploaded Successfully']);
}
Well I would store the file first so that the code would rather looks like:
public function uploadVideo(Request $request) {
$request->validate([
'file' => 'required',
]);
$filename = time();
/* Storing the file on the disk */
$request->file->storeAs('videos', $filename);
/* Recording the upload on the database */
$fileupload = new FileUpload;
$fileupload->filename = $filename;
$fileupload->save();
return response()->json(['success'=>'File Uploaded Successfully']);
}
Indeed, in your code, your are moving a file that isn't stored... It can't work fine.
I would suggests you to read more the documentation about File Storage as you would have to use a command to make a symlink :
php artisan storage:link
I need to send a CSV file through rest api.
I am using reactjs for front end development and constructing backend apis with Laravel.
I am confused with how to send a CSV file through api response ?
Can anyone help me?
this function will download csv file
public function downloadCSV(Request $request){
$table = User::orderBy('created_at','desc')->with('user')->get();
$filename = "users.csv";
$handle = fopen($filename, 'w+');
fputcsv($handle, array('user_name','user_email','country','state','address','zip_code','city'));
foreach($table as $row) {
fputcsv($handle, array(
$row['user_name'],
$row['user_email'],
$row['country'],
$row['state'],
$row['address'],
$row['zip_code'],
$row['city']
));
}
fclose($handle);
$headers = array('Content-Type' => 'text/csv');
return response()->download($filename, 'users.csv', $headers);
}
I am trying to store an image in cakephp 3.0. I am only able to save the filename in db, however, unable to store the actual file on the server. Need help
Form:
echo $this->Form->create('User', array('url' => array('action' => 'create'), 'enctype' => 'multipart/form-data'));
echo $this->Form->input('upload', array('type' => 'file'));
Images controller:
*/
public function add()
{
$image = $this->Images->newEntity();
//Check if image has been uploaded
if(!empty($this->request->data['Images']['upload']['name']))
{
$file = $this->request->data['Images']['upload']; //put the data into a var for easy use
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
//only process if the extension is valid
if(in_array($ext, $arr_ext))
{
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img' . $file['name']);
//prepare the filename for database entry
$this->data['Images']['image'] = $file['name'];
}
}
if ($this->request->is('post')) {
$image = $this->Images->patchEntity($image, $this->request->data);
if ($this->Images->save($image)) {
$this->Flash->success('The image has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The image could not be saved. Please, try again.');
}
}
$this->set(compact('image'));
$this->set('_serialize', ['image']);
}
For those who are looking for the answer just modify this line :
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img' .DS. $file['name']);
DS is a Directory Separator.
Welcome on stackoverflow!
Please check this question:
cakePHP 3.0 uploading images
This will help you, it's a good plugin for uploading images:
http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/
I'm trying to create a simple excel document that contains a 3-column list. (First Name, Last Name, Email Address)
When I output more than 31 rows, a blank excel file is created. Does anyone know how to fix it?
My code below:
$guests = ORM::factory('guest')
->order_by('last_name', 'ASC')
->find_all()
->as_array();
$columns = array('first_name', 'last_name', 'email');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator('Me')
->setLastModifiedBy('Me)
->setTitle('Guest List')
->setSubject('Guest List')
->setDescription('Title of Report, run on ' . date('m/d/Y H:i:s'));
$objPHPExcel->createSheet(0);
$objPHPExcel->setActiveSheetIndex(0);
foreach($guests as $row => $guest) {
$source = array();
$column = 0;
$next_row = $objPHPExcel->getActiveSheet()->getHighestRow();
foreach($guest->as_array() as $column_name => $data) {
if(in_array($column_name, $columns)) {
$objPHPExcel->getActiveSheet()
->setCellValueByColumnAndRow($column, $next_row+1, $data);
$column++;
// unset($data);
}
}
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$this->response->body($objWriter->save('php://output'));
$this->response->send_file(TRUE, 'GuestList.xls');
Kohana has a hard time handling the rendered file using the send_file() method, so write the file to disk using the built-in method from PHPExcel. Then use Kohana to deliver the saved file as a download.
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($filename, 'EXCEL2007');
$this->response->send_file( DOCROOT.$filename );