want to generate pdf from laravel request data - laravel

i tried to generate pdf from my request data function. but it can't pass variable into my pdf generate function. i get requested data from this function.
public function fetch_data()
{
if(request()->ajax())
{
if(request()->from_date != '' && request()->to_date != '')
{
$data = DB::table('sales')
->whereBetween('dlvd_date', array(request()->from_date, request()->to_date))
->get();
} else {
$data = DB::table('sales')->orderBy('dlvd_date', 'desc')->get();
}
echo json_encode($data);
}
}
i want to get this $data value into my pdf generated function like below.
function convert_sales_data_to_html($data)
{
$sales_data = $data;
dd($sales_data);
$output = '';
return $output;
}
and pass to this function
public function pdf($data)
{
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($this->convert_sales_data_to_html($data))->setPaper('a4', 'landscape');
return $pdf->stream();
}
i tried this so many times but show me errors with missing argument. how can i fix this or have another method to generate pdf from requested data in laravel.
this is my routes
Route::post('salesreport/fetch', 'Report\SalesReportController#fetch_data')->name('salesreport.fetch_data');
Route::get('salesreport/pdf', 'Report\SalesReportController#pdf');

Related

How to show POST data from external url api to my blade file

External site data to be displayed on my blade
MerchantCode
MerchantRefNo
Particulars
Amount
PayorName
PayorEmail
Status
RefNo
Here's my Controller
function getPaymentDetail()
{
$data = Http::get('https://external.com/api')->json();
return view('paymentdetails',['data'=>$data]);
}
I would code it like this.
In your controller.
function getPaymentDetail() {
$res = Http::get('https://external.com/api')->json();
$temp_data = json_decode($temp_data, TRUE);
foreach($temp_data as $key => $value) {
$data[$key] = $value;
}
return view('paymentdetails', $data);
}
In your blade file, you can access by {{ $MerchantCode }}

Try to use the codeigniter's file upload library as a general function from Helpers

Can anybody help as I am trying to use the codeigniter's upload library from the helpers folder but I keep getting the same error that I am not selecting an image to upload? Has any body tried this before?
class FileUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'file_uploading'));
$this->load->library('form_validation', 'upload');
}
public function index() {
$data = array('title' => 'File Upload');
$this->load->view('fileupload', $data);
}
public function doUpload() {
$submit = $this->input->post('submit');
if ( ! isset($submit)) {
echo "Form not submitted correctly";
} else { // Call the helper
if (isset($_FILES['image']['name'])) {
$result = doUpload($_FILES['image']);
if ($result) {
var_dump($result);
} else {
var_dump($result);
}
}
}
}
}
The Helper Function
<?php
function doUpload($param) {
$CI = &get_instance();
$CI->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg|png';
$config['file_name'] = date('YmdHms' . '_' . rand(1, 999999));
$CI->upload->initialize($config);
if ($CI->upload->do_upload($param['name'])) {
$uploaded = $CI->upload->data();
return $uploaded;
} else {
$uploaded = array('error' => $CI->upload->display_errors());
return $uploaded;
}
}
There are some minor mistakes in your code, please fix it as below,
$result = doUpload($_FILES['image']);
here you should pass the form field name, as per your code image is the name of file input.
so your code should be like
$result = doUpload('image');
then, inside the function doUpload you should update the code
from
$CI->upload->do_upload($param['name'])
to
$CI->upload->do_upload($param)
because Name of the form field should be pass to the do_upload function to make successful file upload.
NOTE
Make sure you added the enctype="multipart/form-data" in the form
element

What to Show some data On my view but there is a Error In Laravel

Here I am trying to add, Add Comment feature and it's causing a problem to show data.
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
$chatterreplies = Chatterreply::where('chatter_post_id',$request->chatter_post_id)->get();
$chatter_editor = config('chatter.editor');
if ($chatter_editor == 'simplemde') {
// Dynamically register markdown service provider
\App::register('GrahamCampbell\Markdown\MarkdownServiceProvider');
}
echo "<pre>"; print_r($chatterreplies); die;
return view('chatter::discussion', compact('chatterreplies','chatter_editor'))->with('chatter_alert','Add Comment Successfully');
}
And Here Is Where I am passing the variable
$chatter = session()->pull('chatter');
return view('chatter::discussion', compact('discussion', 'posts', 'chatter_editor'))->with('chatterreplies',$chatter);

Laravel: export searched data to the excel

I want to export searched data to the laravel below is my controller
public function getUsageData(Request $request)
{
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$particulars = DB::table('particulars')
->join('reqs', 'particulars.particular_id', "=", 'reqs.particular_id')
->whereBetween('date_applied', [$start_date, $end_date])
->select('particulars.item_name', 'particulars.unit', 'particulars.price', 'reqs.quantity_issued',
DB::raw('particulars.price*reqs.quantity_issued AS total_cost'))
->get();
if ($particulars->isEmpty()) {
return "No Records Found...................... ";
} else {
$pdf = PDF::loadView('issuer.getUsageReport', ['particulars' => $particulars]);
return $pdf->stream('getUsageReport.issuer');
}
}
I want to retrieve those data to the excel instead of pdf please help
Replace the two PDF lines in the else { ... } statement with:
$list = $particulars->toArray();
//This line adds headers if present in your data
array_unshift($list, array_keys($list[0]));
$callback = function() use ($list) {
$FH = fopen('php://output', 'w');
foreach ($list as $row) {
fputcsv($FH, $row);
}
fclose($FH);
};
return response()->streamDownload($callback,"filename.csv");
Depending on the structure of particulars you may need to do some manipulation to the data first, e.g. flattening. Comment below if so.

what is the model code to fetch multiple result set from store procedure in codeigniter?

My Model code is given below.
public function getattendence($data)
{
$sql="CALL `sp_AttendanceDetails`(?,?,?);";
$query=$this->db->query($sql,$data);
return $query->result_array();
}
But it return one result set.The second result set i not get..
Pass data like this
public function getattendence($data)
{
# example of your $data array
$id = $data['id'];
$name = $data['name'];
$date = $data['date'];
$sql="CALL sp_AttendanceDetails($id,'$name',$date);";
$query=$this->db->query($sql);
return $query->result_array();
}
Use return $this->_execute_multi_query($sql);
refer http://php.net/manual/en/mysqli.multi-query.php

Resources