I need to create a csv file with a download button for my table results view. I am using Laravel framework and am unsure on how exactly to do this.
My controller to get the data.
public function reportFailedPayments(){
$startdate = Input::get('startdate');
$enddate = Input::get('enddate');
$status = Input::get('status');
$data = Payment::with('purchase', 'history', 'history.billingInformation', 'purchase.user', 'purchase.productInstance', 'purchase.productInstance.product', 'billingInformation')
->where('Status', '=',$status)->whereBetween('Payment_Date', array($startdate, $enddate))->orderBy('Payment_Date','desc')->get();
return View::make('admin.reports.failedPayments.report', array('payments'=>$data));
}
}
And here is my table view. Just needing some advice on how exactly to create the DL for the csv.
<table class='list'>
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Email</th>
<th>Product Name</th>
<th>Purchase Date</th>
<th>Initial Payment Date</th>
<th>Last Payment Date</th>
<th>Billing Method</th>
<th>Transaction Code</th>
<th>Transaction Message</th>
</tr>
</thead>
<tbody>
#foreach($payments as $p)
<tr>
<td>{{ $p->purchase->user->Last_Name.', '.$p->purchase->user->First_Name }}</td>
<td>{{ $p->purchase->user->Email }}</td>
<td>{{ $p->purchase->productInstance->product->Name }}</td>
<td>{{ $p->purchase->Purchase_Date }}</td>
<td>{{ $p->Payment_Date }}</td>
<td>{{ $p->lastHistory()->Process_Time }}</td>
<td>{{ $p->lastHistory()->billingInformation->Payment_Type == PaymentType::PayPal ? "PayPal" :
CreditCardType::ToString($p->lastHistory()->billingInformation->CCType)." Ending In ".$p->lastHistory()->billingInformation->CCLast4 }}</td>
<td>{{ $p->lastHistory()->Trans_Response_Code }}</td>
<td>{{ $p->lastHistory()->Trans_Response_Text }}</td>
</tr>
#endforeach
</tbody>
</table>
Let me start by saying that I won't write all the code for you. This just isn't the purpose of StackOverflow...
However I hope I can point you in the right direction with this answer.
Your problem can be split in two tasks. One being generating the CSV file and then serve the download.
Generating the CSV file
You'll find a lot of code snippets on the internet. Here's one I found and modified a bit
$csvData = $yourCollection->toArray();
$filename = tempnam(sys_get_temp_dir(), 'csv_'); // create a temporary file in the system's default tmp directory
$file = fopen($filename, 'w');
foreach($csvData as $row){
fputcsv($file, $row, ';'); // 3rd parameter is optional (default: ',')
}
fclose($file);
Now we have a csv file in our temp folder, ready to be downloaded. Laravel gives you a very way to download files.
Serving the file for download
In your controller action:
$response = Response::download($filename);
unlink($filename); // lets not forget to delete the file or they will pile up in your temp folder
return $response;
Maybe you also need to send some additional headers (e.g. for specifying a filename that the user sees) but I'll leave that up to you.
And here are some references to the functions used in the code:
Laravel Response::download
PHP fputcsv()
PHP tempnam()
Related
This question already has answers here:
Variable undefined error in laravel blade view
(4 answers)
Closed last month.
I want display my data to my blade file but it show this error
Undefined variable $consultation
This is my controller
public function p_consultation()
{
$consultation = Consultation::all();
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
}
This is my Route
Route::get('dashboard/admin_panel/p_consultation', [PenconsultationController::class, 'Consultation'])
And this is my Blade File
<tr>
<th>#</th>
<th>Item Name</th>
<th>Dscriptiom</th>
<th>Material</th>
<th>Quantity</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
</thead>
<tbody>
#foreach($consultation as $consultation)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $consultation->item_name }}</td>
<td>{{ $consultation->description }}</td>
<td>{{ $consultation->material }}</td>
<td>{{ $consultation->quantity }}</td>
<td>{{ $consultation->start_date }}</td>
<td>{{ $consultation->end_date }}</td>
</tr>
#endforeach
change from
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
to
return view ('dashboard/admin_panel/p_consultation', ['consultation' => $consultation]);
If you're passing data to the controller using with(), you need to bind it with an accessor.
->with('consultation', $consultation)
return view('dashboard/admin_panel/p_consultation')->with('consultation', $consultation);
You can use compact() as well
Example.
return view('greeting')
->with('name', 'Victoria')
->with('occupation', 'Astronaut');
Hello I cannot un delete my data why is it? here is my code
on the Controller
public function displayArchive()
{
$clients = Client::withTrashed();
return view('admin.clients.homeArchive')->with('clients', $clients);
}
one the View
<table class="table table-bordered" id="dynamic_field">
<tr>
<th>Client Code</th>
<th>Client Name</th>
<th>Address</th>
<th>Tel No.</th>
<th>Contact Person</th>
<th>Mobile No.</th>
<th>Email Address</th>
<th>Website</th>
<th>Status</th>
<th>Update</th>
</tr>
#foreach ($clients as $client)
<tr>
<td>{{ $client->client_code }}</td>
<td>{{ $client->client_name }}</td>
<td>{{ $client->address }}</td>
<td>{{ $client->tel_no }}</td>
<td>{{ $client->contact_person }}</td>
<td>{{ $client->mobile_no }}</td>
<td>{{ $client->email_ad }}</td>
<td>{{ $client->website }}</td>
<td>Inactive</td>
<td></td>
</tr>
#endforeach
</table>
on the Web
Here you can see how I call my controller and view pages.
Route::get('/admin/clients/homeArchive', 'Admin\ClientsController#displayArchive');
EDITED
Here is the edited code please take a look
my Model
use SoftDeletes;
protected $dates = ['deleted_at'];
// Table Name
protected $table = 'clients';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
Can you try with ->get() method.
public function displayArchive()
{
$clients = Client::withTrashed()->get();
return view('admin.clients.homeArchive')->with('clients', $clients);
}
NOTE: it don't undelete any data. it only get data with deleted rows.
If you want recover deleted data, you must use ->restore() method.
For all restored all data;
Link:
Inactive
Route:
Route::get('/admin/clients/restore-all', 'Admin\ClientsController#restoreAll')->name('admin.client.restore_all');
Controller action:
public function restoreAll(){
Client::withTrashed()->restore();
}
Restore row by row data;
Link:
Inactive
Route:
Route::get('/admin/clients/restore/{client}', 'Admin\ClientsController#restore')->name('admin.client.restore');
Controller action:
public function restore(Client $client){
$client->restore();
}
$client->id is the client collection, I think you want to inactive in the listed foreach row, is right?
I'm new to Laravel and i'm trying to export to excel sheet every ticket created for the past 7 days starting from current day into an excel table with headers.
my excelexport code in TicketsController is :
public function exportxls(){
$weeklyTickets = \App\Tickets::whereBetween('created_at', array(date("Y-m-d", strtotime("-7 days")), date('Y-m-d')))->get();
Excel::create('tickets', function($excel) use ($weeklyTickets){
$excel->sheet('tickets', function($sheet) use ($weeklyTickets){
$sheet->loadView('export.ticketsexcel', array('weeklyTickets' => $weeklyTickets));
})->export('xls');
});
return redirect('/');
}
My Tickets export view is:
<table>
<thead>
<tr>
<td>Nom de la société</td>
<td>Intervenant</td>
<td>Type D'assistance</td>
<td>Objet</td>
<td>Urgence</td>
<td>Statut</td>
<td>Utilisateur</td>
</tr>
</thead>
<tbody>
#foreach ($weeklyTickets as $ticket)
<tr>
<td>{{ $ticket->societe}}</td>
<td>{{ $ticket->intervenant}}</td>
<td>{{ $ticket->assistance->level}}</td>
<td>{{ $ticket->message}}</td>
<td>{{ $ticket->urgence->niveau}}</td>
<td>{{ $ticket->statut}}</td>
<td>{{ $ticket->utilisateur->name}}</td>
</tr>
#endforeach
</tbody>
</table>
Can you help me figure out why it only exports 1 ticket instead of all tickets of the past 7 days, and why it doesn't make it into an excel table in the sheet?
Thanks in advance.
I want to list down the users who are registered in last 5 day.. To list down all the users 1 use following query..
$users = User::select('*')->orderby('created_at', 'desc')->get();
and fetch it in view like this.
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td>{{ \App\Points::getUserPoints($u->id) }}</td>
</tr>
<?php }?>
</tbody></table>
but what will be the query to fetch users who have registered in last 5 days..?? any one please tell me a good query..
You can use Carbon (docs) to do this:
$now = Carbon::now();
$users = User::where('created_at', '>=', $now->subDays(5))->get();
Make sure to "use Carbon\Carbon;" at the top of the file.
I am developing a project in laravel 5.3. where I am using a users table and a points table in database. users are associated with transactions of points. these transections are recorded in points table. my points table looks like in my previous question who's link is this
-- I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their --
. Now I am fetching users' all data in a page whare I want to show the net balance of there points too. you can see the result in image.image is here
so currently I am using the following code in controller.
public function users_list()
{
$ptitle = "Website Users";
$pdesc = "";
//$total_users = User::count();
$users = User::select('*')->orderby('created_at', 'desc')->get();
return view('admin.all-users-list',
['ptitle' => $ptitle,
'pdesc' => $pdesc,
'users' => $users,
'total_users' => $this->total_users,
]);
}
And getting result in view like following
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td></td>
</tr>
<?php }?>
</tbody></table>
But I dont know how can i create a join in query builder to fetch net points for every user in list