How to remove blank line in csv file using laravel maatwebsite - php

I want to export csv file using Maatwebsite in laravel. The process is running well, but in the csv file contains blank line in last data when i open it using notepad. Displayed at image below
This is the function to get the data
public function view(): View
{
$data = DB::table('transaction as a')
->join('transaction_details as b', 'a.id', 'b.header_id')
->select('b.order_number', 'b.changed_number')
->whereIn('a.id', $this->id)
->where('a.approve', 1)
->get();
return view('vendor.voyager.transaction.excel-csv.generate-csv', [
'data' => $data
]);
}
public function getCsvSettings(): array
{
return [
'enclosure' => ''
];
}
This is function to call the function above
public function generate(Request $request)
{
$id = $request->ids;
return Excel::store(new ExportTransaction($id), 'test.csv', 'ftp');
}
How to solve my problem to remove blank line in the csv fie

Related

laravel : Error Call to a member function all() on array

This is used to export excel
My controllers:
public function reportExport(Request $request)
{
return Excel::download(new ReportExport($request), 'LAPORAN ABSENSI.xlsx');
}
Export folder : ReportExport
return [
"att_cross" => $att_cross,
"belum_absen" => $belum_absen,
"absen" => $absen,
];
but, when I return one of the data is successful, for example :
return $absen;
I want to display the contents of the variable $absent, $belum_absen, $att_cross but I get an error message "Call to a member function all() on array "
how can I display the data for these three parameters?
Read the docs for Excel package, you need to map() the output.

Too few arguments to function when downloading a PDF

I am trying to generate a PDF with some details of an individual user using the Barryvdh DomPDF library but having some problems trying to generate it.
Controller method:
public function downloadPDF(Card $card)
{
$user = User::find($card->user_id);
$pdf = (new \Barryvdh\DomPDF\PDF)->loadView('pdf/cardsReport', $user);
return $pdf->download('cards.pdf');
}
Here is how I am referencing the route.
Download PDF
Route:
$router->get(
'/downloadPDF/{card}',
[
'as' => 'get::admin.download-pdf',
'uses' => 'EditCardController#downloadPDF',
]
);
I get this error:
Type error: Too few arguments to function
Barryvdh\DomPDF\PDF::__construct(), 0 passed in EditCardController.php
and exactly 4 expected.
I'm confused by this as I've seen many samples of using pdf and laravel where you don't need to pass four arguments so was wondering why this would be?
According to documentation, you should use facade instead of constructor:
public function downloadPDF(Card $card)
{
$user = User::find($card->user_id);
$pdf = PDF::loadView('pdf/cardsReport', $user);
return $pdf->download('cards.pdf');
}
Kindly use below format
$data = [
'title' => 'Welcome to ItSolutionStuff.com',
'date' => date('m/d/Y'),
];
$pdf = App::make('dompdf.wrapper');
$pdf->loadView('bill',$data);
return $pdf->stream();

Laravel download files with two tables in database

I hope so im getting close to final stage. I uploading file to storage/files/ and create uniq folder for each upload file with id_message without problem and store file data in table files
Final path of file is /storage/files/{id_message}/{file_name} both variables id_message and file_name are in table files.
FileController function for fileUpload:
function fileUpload(Request $request)
{
$request->validate([
'id_message' => 'required|min:6'
]);
$idParameter = $request->id_message=$request->id_message;
$result=$request->file('file_path')->store('files/'.$idParameter);
$file = new File;
$file->id_message=$idParameter;
$file->file_path=$result;
$file->file_name=$request->file('file_path')->getClientOriginalName();
$file->save();
after upload i have this data in table files:
id id_message file_name
1 000001 Myfile.zip
/storage/app/files/000001/Myfile.zip
FileController : getDownload
public function getDownload($id)
{
$resultFile = DB::table('files')->where('id_message',$id)->first();
$attachment = store_path().'/' . $resultFile->id_message . '/' . $resultFile->file_name;
return response()->download($attachment);
}
route
Route::get('download/{id}/{fileName}', 'FileController#getDownload')->name('downloadFile');
view.blade
<td>Download</td>
error
Undefined variable:resultFile
do i getting closer to finaly download file in laravel ?
controller for view table users
public function postLogin(Request $request)
{
request()->validate([
'id_message' => 'required',
'sms_code' => 'required',
]);
$credentials = $request->only('id_message', 'sms_code');
$request->session()->flash('success','');
if ($user=User::where($credentials)->first()) {
auth()->login($user);
return redirect()->intended('dashboard')->with('success','');
}
return Redirect::to("login")->with(['url_attribute' => $url_attribute,'id_message' => $id_message])->with('error','');
}
public function dashboard()
{
if(Auth::check())
{
return view('dashboard');
}
return Redirect::to("login")->withSuccess('Opps! You do not have access');
}
Leading zeros may be getting stripped if its converted to integer.
Try adding padding to $id once it's passed to getDownload:
str_pad($id, 6, '0', STR_PAD_LEFT);

Call to a member function all() on array laravel excel

i'm using laravel maatwebsite excel,
i tried passing variable and do some action and return as array to export,
So i get the Error when i tried to pass array to the main controller for excel export(download)
my main controller
public function excel_export(Request $request){
return Excel::download(new UsersExport($request->exp_vlas), 'users.xlsx');
}
here iam passing variable to the collection
my export controller
public function collection(){
$instruments = implode(",",$this->id);
$instruments = explode(",",$instruments);
//$i=0;
foreach ($instruments as $instrument) {
$instr_list = DB::table('instruments')->select('*')->where('id',$instrument)->get()->toArray();
$arr_instrulist[] = $instr_list;
$instrument_var[] = $instrument;
$instr_list = "";
//$i++;
}
$arr_instrulist_excel[] = array('Instrument Name', 'Serial', 'Qa id', 'Unit', 'Range');
foreach($arr_instrulist as $arr_instrulists){
//$arr_instrulists = array($arr_instrulists);
$arr_instrulist_excel[] = array(
'Instrument Name' => $arr_instrulists[0]->instrument_name,
'Serial' => $arr_instrulists[0]->serial,
'Qa id' => $arr_instrulists[0]->qa_identification_no,
'Unit' => $arr_instrulists[0]->unit,
'Range' => $arr_instrulists[0]->range
);
}
return $arr_instrulist_excel;
}
when tried to return this($arr_instrulist_excel) i get an error
please give me some solution for this.
Error i'm facing
You are telling export script to get your data from Collection but you are giving an array. You should return collection instead.
You can simply wrap your array in collection like that:
return collect($arr_instrulist_excel);

Custom functions in Laravel

I use Maatwebsite/Laravel-Excel package to upload CSV to DB. Before entering data to DB i wanna do some additional checks with the database. Something like Project_id available in the Project table. Where do i write the required checking function. Is it a proper way to write it in the same controller. I have seen some people create folder In app\Example and write function file in it
UPDATE :
please let me know is this a proper way to do
https://www.youtube.com/watch?v=fS8q_sD1sZM
This the sample CSV format
This the controller function
public function uploadDealerCSV()
{
if(Input::hasFile('file')){
$path = Input::file('file')->getRealPath();
$data = Excel::load($path, function ($reader) {
$reader->ignoreEmpty();
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'project_id' => $value->project_id,
'dealer_bl_code' => $value->bl_code,
'reporgroup' => 104,
'added_by' => 73,
'updated_at' => '2017-08-08 10:34:54',
];
}
if(!empty($insert)){
DB::table('cts_project_list')->insert($insert);
//dd('Insert Record successfully.');
}
}
}
return back();
}
public function testFunction(){
}

Categories