我在laravel中尝试生成pdf的朋友,我可以查看我的数据,但不能生成pdf。问题是什么。请帮我。给我的控制器`
<?php
namespace App\Http\Controllers;
//use Illuminate\Http\Request;
use DB;
use PDF;
use Excel;
use View;
use Request;
class allUserPdfController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('allUserPersonalCheckBox');
}
public function allUserPdf(Request $request)
{
$users = DB::table('users')->get();
$allUsersData = array();
foreach ($users as $user) {
$user_id = $user->id;
$education = DB::table('education')->where('user_id' ,'=', $user_id)->get();
$publication = DB::table('publications')->where('user_id' ,'=', $user_id)->get();
$supervision = DB::table('supervisions')->where('user_id' ,'=', $user_id)->get();
$fyproject = DB::table('fyprojects')->where('user_id' ,'=', $user_id)->get();
$userAllData=array( 'user'=>$user, 'education'=>$education,'publication'=>$publication, 'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
return View::make("pdf/allUserPersonalPdf")->with("allUsersData",$allUsersData);
}
public function allUserCheckBoxPdf(Request $request)
{
$users = DB::table('users')->get();
$allUsersData = array();
$per = Request::get('per');
$edu = Request::get('edu');
$pub = Request::get('pub');
$sup = Request::get('sup');
$fyp = Request::get('fyp');
foreach ($users as $user) {
$user_id = $user->id;
$education = DB::table('education')->where('user_id' ,'=', $user_id)->get();
$publication = DB::table('publications')->where('user_id' ,'=', $user_id)->get();
$supervision = DB::table('supervisions')->where('user_id' ,'=', $user_id)->get();
$fyproject = DB::table('fyprojects')->where('user_id' ,'=', $user_id)->get();
if($per && $edu && $pub && $sup && $fyp ){
$userAllData=array( 'user'=>$user, 'education'=>$education,'publication'=>$publication, 'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per && $edu && $pub && $sup ){
$userAllData=array( 'user'=>$user, 'education'=>$education,'publication'=>$publication, 'supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if($per && $edu && $pub && $fyp ){
$userAllData=array( 'user'=>$user, 'education'=>$education,'publication'=>$publication,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per && $edu && $sup && $fyp ){
$userAllData=array( 'user'=>$user, 'education'=>$education,'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per && $pub && $sup && $fyp ){
$userAllData=array( 'user'=>$user,'publication'=>$publication, 'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($edu && $pub && $sup && $fyp ){
$userAllData=array('education'=>$education,'publication'=>$publication, 'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per && $edu && $pub ){
$userAllData=array( 'user'=>$user, 'education'=>$education,'publication'=>$publication);
array_push($allUsersData, $userAllData);
}
else if($per && $edu && $sup ){
$userAllData=array( 'user'=>$user, 'education'=>$education,'supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if($per && $edu && $fyp ){
$userAllData=array( 'user'=>$user, 'education'=>$education,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per && $pub && $sup){
$userAllData=array( 'user'=>$user,'publication'=>$publication, 'supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if($per && $pub && $fyp){
$userAllData=array( 'user'=>$user,'publication'=>$publication,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per && $sup && $fyp){
$userAllData=array( 'user'=>$user,'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($edu && $pub && $sup){
$userAllData=array('education'=>$education,'publication'=>$publication, 'supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if($edu && $sup && $fyp){
$userAllData=array('education'=>$education,'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($pub && $sup && $fyp){
$userAllData=array('publication'=>$publication, 'supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per && $edu ){
$userAllData=array( 'user'=>$user, 'education'=>$education);
array_push($allUsersData, $userAllData);
}
else if($per && $pub ){
$userAllData=array( 'user'=>$user,'publication'=>$publication);
array_push($allUsersData, $userAllData);
}
else if($per && $sup ){
$userAllData=array( 'user'=>$user,'supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if($per && $fyp ){
$userAllData=array( 'user'=>$user,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($edu && $pub ){
$userAllData=array('education'=>$education,'publication'=>$publication);
array_push($allUsersData, $userAllData);
}
else if($edu && $sup ){
$userAllData=array('education'=>$education,'supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if($edu && $fyp ){
$userAllData=array('education'=>$education,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if( $pub && $sup){
$userAllData=array('publication'=>$publication, 'supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if( $pub && $fyp){
$userAllData=array('publication'=>$publication,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($sup && $fyp){
$userAllData=array('supervision'=>$supervision,'fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else if($per){
$userAllData=array( 'user'=>$user);
array_push($allUsersData, $userAllData);
}
else if($edu){
$userAllData=array('education'=>$education);
array_push($allUsersData, $userAllData);
}
else if($pub){
$userAllData=array('publication'=>$publication);
array_push($allUsersData, $userAllData);
}
else if($sup){
$userAllData=array('supervision'=>$supervision);
array_push($allUsersData, $userAllData);
}
else if($fyp){
$userAllData=array('fyproject'=>$fyproject);
array_push($allUsersData, $userAllData);
}
else {
return view('pdf/personalpdf');
}
}
//return View::make("pdf/allUserPersonalPdf")->with("allUsersData",$allUsersData);
// $pdf = PDF::loadView("pdf/allUserPersonalPdf")->with("allUsersData",$allUsersData);
// return $pdf->stream('allusersinfo.pdf');
$pdf = PDF::loadView("pdf/allUserPersonalPdf", compact('"allUsersData",$allUsersData'));
return $pdf->download('personalinfo.pdf');
}
// public function allUserPdf(Request $request)
// {
// $userInfo = DB::table('users')->get();
// $education = DB::table('education')->get();
// $publication = DB::table('publications')->get();
// $supervision = DB::table('supervisions')->get();
// $fyproject = DB::table('fyprojects')->get();
// $pdf = PDF::loadView('pdf/allUserPdf', compact('userInfo','education','publication','supervision','fyproject'));
// return $pdf->stream('allUserinfo.pdf');
// return view('pdf/allUserPdf');
// }
function excel(){
$userInfo = DB::table('users')->get()->toArray();
$userArray[]= array('name','fname','phone','email','off_email');
foreach ($userInfo as $user) {
$userArray[] = array(
'name' => $user->name,
'fname' => $user->fname,
'phone' => $user->phone,
'email' => $user->email,
'off_email' => $user->off_email,
);
}
Excel::create('User Info', function($excel) use ($userArray)
{
$excel->setTitle('User Info');
$excel->sheet('User Info', function($sheet) use ($userArray){
$sheet->fromArray('$userArray',null,'A1',false,false);
});
})->export('xlsx');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}`
这是我的查看代码。我可以用简单的视图查看结果,但是在pdf生成中有问题。它给了我错误的未定义变量:allUsersData(视图:C:\ xampp \ htdocs \ plearning \ resources \ views \ pdf \ allUserPersonalPdf.blade.php)。
这是我查看文件的代码。
@extends('layouts.style')
@section('content')
<!DOCTYPE html>
<html>
<body>
<h1>All Users Data</h1>
@if(count($allUsersData) > 0)
@foreach($allUsersData as $usersData)
<div class="container">
@php
$user = isset($usersData['user']) ? $usersData['user'] : '' ;
@endphp
@isset($user)
@if(!empty($user))
<h2 class="text-success text-center">Profile</h2>
<table class="table table-bordered" border="1px">
<tr>
<th>Name</th>
<th>father Name</th>
<th>contact</th>
<th>Email</th>
<th>Official Email</th>
</tr>
<tr>
<td>{{$user->name}}</td>
<td>{{$user->fname}}</td>
<td>{{$user->phone}}</td>
<td>{{$user->email}}</td>
<td>{{$user->off_email}}</td>
</tr>
</table><br>
@endif
@endisset
@php
$userEducation = isset($usersData['education']) ? $usersData['education'] : '' ;
@endphp
@isset($userEducation)
@if(!empty($userEducation))
<h2 class="text text-success text-center">
Education Details
</h2>
<table class="table table-bordered" border="1px">
<tr>
<th>
Degree
</th>
<th>University</th>
<th>Country</th>
<th>Year</th>
<th>Research Area</th>
</tr>
@foreach($userEducation as $value)
<tr>
<td>{{$value->degree}}</td>
<td>{{$value->univ}}</td>
<td>{{$value->country}}</td>
<td>{{$value->year}}</td>
<td>{{$value->research_area}}</td>
</tr>
@endforeach
</table><br>
@endif
@endisset
@php
$userPublication = isset($usersData['publication']) ? $usersData['publication'] : '' ;
@endphp
@isset($userPublication)
@if(!empty($userPublication))
<h2 class="text text-success text-center">
Publications Details
</h2>
<table class="table table-bordered" border="1px">
<tr>
<th>
Title
</th>
<th>Status</th>
<th>Year</th>
</tr>
@foreach($userPublication as $value)
<tr>
<td>{{$value->title}}</td>
<td>{{$value->status}}</td>
<td>{{$value->year}}</td>
</tr>
@endforeach
</table><br>
@endif
@endisset
@php
$userSupervision = isset($usersData['supervision']) ? $usersData['supervision'] : '' ;
@endphp
@isset($userSupervision)
@if(!empty($userSupervision))
<h2 class="text text-success text-center">
Supervisions Details
</h2>
<table class="table table-bordered" border="1px">
<tr>
<th>
Student Name
</th>
<th>Semester</th>
<th>Research Area</th>
<th>Course Work</th>
</tr>
@foreach($userSupervision as $value)
<tr>
<td>{{$value->sname}}</td>
<td>{{$value->semester}}</td>
<td>{{$value->research_area}}</td>
<td>{{$value->cwork}}</td>
</tr>
@endforeach
</table><br><br>
@endif
@endisset
@php
$userFyproject = isset($usersData['fyproject']) ? $usersData['fyproject'] : '' ;
@endphp
@isset($userFyproject)
@if(!empty($userFyproject))
<h2 class="text text-success text-center">
Fyprojects Details
</h2>
<table class="table table-bordered" border="1px">
<tr>
<th>
Degree
</th>
<th>Groups Name</th>
<th>Project Title</th>
</tr>
@foreach($userFyproject as $value)
<tr>
<td>{{$value->degree}}</td>
<td>{{$value->gname}}</td>
<td>{{$value->ptitle}}</td>
</tr>
@endforeach
</table><br>
@endif
@endisset
</div>
<hr><br>
@endforeach
@else
<h2>No Data Found</h2>
@endif
</body>
</html>
@endsection