how to save data in word file from database in laravel - laravel-5

I want to create a word file as a report. I want to fetch all data from the table and put to the word file but I am not able to save data to the file. I am doing in laravel 5.2. File is getting created and downloaded but data which is coming from database is not getting saved. thanks everyone
Here is my controller code -
public function downloadAbleFile(Request $request){
//Fetching all records based on projectid
$project_id = $request->input('project_id');
$questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();
$headers = array(
"Content-type"=>"text/html",
"Content-Disposition"=>"attachment;Filename=report.doc"
);
$content = '<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>
<table>
<thead>
<tr>
<th>Project ID</th>
<th>Question Number</th>
<th>User ID</th>
<th>Answer</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
function project(){
foreach ($questiondetails as $question){
return(
echo "<tr>";
echo "<td>".$question->project_id."</td>";
echo "<td>".$question->question_num."</td>";
echo "<td>".$question->user_id."</td>";
echo "<td>".$question->answer."</td>";
echo "<td>".$question->status."</td>";
echo "</tr>";
);
}
}
project();
?>
</tbody>
</table>
</p>
</body>
</html>';
return Response::make($content,200, $headers);
}

Why do not you use blade?
Try this
in your controller:
public function downloadAbleFile(Request $request){
$project_id = $request->input('project_id');
$questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();
$headers = array(
"Content-type" => "text/html",
"Content-Disposition" => "attachment;Filename=report.doc"
);
$content = View::make('path.view', [
'questiondetails' => $questiondetails,
])->render();
return Response::make($content,200, $headers);
}
in your view
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<br>
<table>
<thead>
<tr>
<th>Project ID</th>
<th>Question Number</th>
<th>User ID</th>
<th>Answer</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach($questiondetails as $question)
<tr>
<td>{{ $question->project_id }}</td>
<td>{{ $question->question_num }}</td>
<td>{{ $question->user_id }}</td>
<td>{{ $question->answer }}</td>
<td>{{ $question->status }}</td>
</tr>
#endforeach
</tbody>
</table>
<br>
</body>
</html>

Related

How to solve design break when use page-break in dompdf

When generating the pdf with page-break-before after 3rd rows the design is breaking down. Here is my code
<!DOCTYPE html>
<html lang="en">
<head>
<title>DOM-PDF</title>
</head>
<body>
<table style="width:100%;">
<thead>
<tr>
<th>Serial</th>
<th>Name</th>
<th>Roll</th>
<th>Info</th>
</tr>
</thead>
<tbody>
#php
$n = 0;
for($i=0;$i<10;$i++){
#endphp
<tr style="text-align:center;">
<td>1</td>
<td>Rashedul Hasan</td>
<td>1208039</td>
<td>I am an employee.</td>
</tr>
#php
if($n==3){
echo "<i style='page-break-before:always;'></i> ";
}
$n++;
}
#endphp
</tbody>
</table>
</body>
</html>
This is the pdf view of page 1
This is the pdf view of page 2
In page 2 second row is moving to the right. How to fix it?
What about using chunks instead of breaking the table?
{{-- Replace collect()->times(10) with a collection when you're getting the actual data from DB --}}
#foreach (collect()->times(10)->chunk(3) as $chunk)
<table style="width:100%;">
<thead>
<tr>
<th>Serial</th>
<th>Name</th>
<th>Roll</th>
<th>Info</th>
</tr>
</thead>
<tbody>
#foreach($chunk as $result)
<tr style="text-align:center;">
<td>{{ $result }}</td>
<td>Rashedul Hasan</td>
<td>1208039</td>
<td>I am an employee.</td>
</tr>
#endforeach
</tbody>
</table>
<i style='page-break-before:always;'></i>
#endforeach

Return Action Button Shaped Text Datatable Laravel

I want to add action button to my datatable here the code
html table and datatables
<table id="users-table" class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>alamat</th>
<th>nohp</th>
<th>action</th>
</tr>
</thead>
</table>
<script id="script">
$(function () {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: 'test/json'
});
});
</script>
heres the laravel json datatables
public function data(Datatables $datatables)
{
$builder = Kontak::query()->select('id', 'nama', 'email', 'alamat', 'nohp');
return $datatables->eloquent($builder)
->addColumn('action', 'users.datatables.intro')
->rawColumns(['action'])
->make();
}
but it keeps show result like this
result images
You should try ->escapeColumns([]) before ->addColumn('action', 'users.datatables.intro')
You can use the routes methods for your model.
view :
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>alamat</th>
<th>nohp</th>
<th>action</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->alamat }}</td>
<td>{{ $user->nohp }}</td>
<td>
<form action="{{ route('users.destroy' , ['id' => $user->id]) }}" method="post">
{{ method_field('delete') }}
{{ csrf_field() }}
<div class="btn-group btn-group-xs">
edit
<button type="submit" id="deleteButton" data-name="{{ $user->id }}" class="btn btn-xs btn-warning">delete</button>
</div>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
controller:
public function index()
{
$users = User::latest()->paginate(25);
return view('users.all' , compact('users'));
}
change users.all to your users view.

Use button inside controller

I am trying to search users in a panel with ajax. But along with that, I also want to edit the selected user.
I have created the button inside the controller to achieve this but the button isn't working.
Here is my controller code:
public function searchTeacher(Request $request){
if($request->ajax()){
$output = '';
$query = $request->get('query');
if($query != '')
{
$data = DB::table('users')->where('name', $query)->get();
}else{
$data = DB::table('users')->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->name.'</td>
<td>'.$row->email.'</td>
<td>'.$row->teacherId.'</td>
<td>'.$row->subject.'</td>
<td> <button href={{route(update-teacher-view), '.$row->id.'}}> Edit</button> </td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($data);
}
}
Here is the ajax for search method-
<script>
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
console.log(query)
$.ajax({
url:"{{ route('search-teacher') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query);
});
});
Here is my html-
<section class="panel">
<table class="table table-striped">
<thead class="team-list chat-list-side info border-less-list">
<th>Teacher Name</th>
<th>Email</th>
<th>Teacher Id</th>
<th>Subject</th>
</thead>
<tbody>
</tbody>
</table>
</section>
A solution will be appreciated.
Thanks in advance.
Maybe it's a wrong string interpolation? Should be like this:
<?php
$output .= '
<tr>
<td>'.$row->name.'</td>
<td>'.$row->email.'</td>
<td>'.$row->teacherId.'</td>
<td>'.$row->subject.'</td>
<td> <button href='.route('update-teacher-view', $row->id).'> Edit</button> </td>
</tr>
';
Also instead of echo-ing, just return the value, it will be automatically converted to JSON response:
// echo json_encode($data); // Change this line to below
return $data;
Render the blade file with the data this is the best solution.
In controller
return View::make('ajaxFilter',compact('data'))->render();
On ajaxFilter.blade.php
<section class="panel">
<table class="table table-striped">
<thead class="team-list chat-list-side info border-less-list">
<th>Teacher Name</th>
<th>Email</th>
<th>Teacher Id</th>
<th>Subject</th>
</thead>
<tbody>
#if($data->count() > 0)
#foreach($data as $row)
<tr>
<td>{{ $row->name }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->teacherId }}</td>
<td>{{ $row->subject }}</td>
<td>
Edit //button design
</td>
</tr>
#endforeach
#else
No data Found
#endif
</tbody>
</table>
</section>
On ajax response, you can append HTML data something like that
$('#div_or_section_id').html(data);

Laravel Excel Export - From View not working

I tried to implement exporting to excel file From View approach using the Laravel Excel. Here is the link of the documentation https://laravel-excel.maatwebsite.nl/3.1/exports/from-view.html. But I can't figure it out yet referencing the example shown in the website. It returns an error saying PhpOffice \ PhpSpreadsheet \ Writer \ Exception
Invalid parameters passed. . I've been changing my codes trying to solve this but no luck at all. Please help me figure this out. Below are my codes. Thank you.
LoansExport.php
<?php
namespace App\Exports;
use App\Loan;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class LoansExport implements FromView
{
public function view(): View
{
return view('partials.view_loan_export', [
'loans' => Loan::all()
]);
}
}
view_loan_export.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
</body>
LoansController.php
<?php
namespace App\Http\Controllers;
use App\Loan as Loan;
use App\Member as Member;
use Illuminate\Http\Request;
use App\Exports\LoansExport;
use Maatwebsite\Excel\Facades\Excel;
class LoansController extends Controller
{
public function loanexport()
{
return Excel::download(new LoansExport, 'loans.xlsx');
}
}
web.php
Route::get('/loanexport', 'LoansController#loanexport');
error
I have done By different way
LoansController.php
public function loanexport(){
$loan= array();
$loans= loan::all();
$data = [
'success' => 'success',
'loans' => $loans,
];
return Excel::download(new LoansExport($data), 'loans.xlsx');
}
LoansExport.php
public function __construct($data) {
$this->data = $data;
}
public function view(): View
{
//dd($this->data);
return view('partials.view_loan_export',$this->data);
}
view_loan_export.blade.php
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
just put the table tag and the tag within it in your view
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>

Why syntax error, unexpected 'endforeach' (T_ENDFOREACH) in Laravel 5.3?

i have created a view form blade template for showing database data. Now running the script showing fatal error for foreach loop when i ending foreach loop by #endforeach, what will be the solution
<!DOCTYPE html>
<html>
<head>
<title>All userinfo</title>
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row col-md-6 col-md-offset-2 custyle">
<table class="table table-striped custab">
<thead>
<b>+</b> Add new categories
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Hometown</th>
<th class="text-center">Action</th>
</tr>
</thead>
foreach($alldata as $data)
<tr>
<td>{{ $data -> id }}</td>
<td>{{ $data -> name }}</td>
<td>{{ $data -> email }}</td>
<td>{{ $data -> age }}</td>
<td>{{ $data -> hometown }}</td>
<td class="text-center"><a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Edit</a> <span class="glyphicon glyphicon-remove"></span> Del</td>
</tr>
#endforeach
</table>
</div>
</div>
</body>
</html>
Here you forgot about #:
#foreach($alldata as $data)

Resources