我很困惑,为什么我无法将其导出为excel。请帮我。这是一个错误,它无法读取“全部”。 这是我在UsersController中的代码
public function userExport()
{
Excel::create('data_function',function($excel){
$excel->sheet('mysheet', function($sheet){
$sheet->loadView('user.list');
});
})->download('xls');
}
然后在我的user / list.blade
中<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Employees
<small>@lang('app.list_of_registered_users') - {{ $all }}</small>
</h1>
</div>
</div>
@include('partials.messages')
<form method="GET" action="" accept-charset="UTF-8" id="users-form">
<div class="row tab-search">
<div class="col-md-2">
<div class="form-group-sm">
<select class="form-control form-control-sm" id="company" name="company">
<option selected disabled>Company</option>
@foreach ($companies as $company)
@if (($company->id)=="1")
<option>All</option>
@else
<option value="{{ $company->id }}">{{ $company->name }}</option>
@endif
@endforeach
</select>
</div>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-warning btn-sm btn-block" id="filter-user">
<i class="glyphicon glyphicon-filter"></i>
Filter Employee
</button>
</div>
<div class="col-md-1">
<a href="{{ route('user.export') }}" class="btn btn-sm btn-success btn-block">
<i class="fa fa-file-excel-o"></i>
Excel
</a>
</div>
<div class="col-md-2">
<a href="{{ route('user.create') }}" class="btn btn-primary btn-sm btn-block" id="add-user">
<i class="glyphicon glyphicon-plus"></i>
Add
Employee
</a>
</div>
</div>
<div class="row tab-search"> <div class="col-md-2">
<div class="form-group-sm">
<select class="form-control form-control-sm" id="benefit" name="benefit">
<option selected disabled>Benefits</option>
@foreach ($benefits as $benefit)
@if (($benefit->id)=="1")
<option>All</option>
@else
<option value="{{ $benefit->id }}">{{ $benefit->name }}</option>
@endif
@endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group-sm">
<div class="input-group custom-search-form-sm">
<input type="text" class="form-control form-control-sm" name="search" value="{{ Input::get('search') }}" placeholder="Search Name">
<span class="input-group-btn">
<button class="btn btn-default btn-sm" type="submit" id="search-users-btn">
<span class="glyphicon glyphicon-search"></span>
</button>
@if (Input::has('search') && Input::get('search') != '')
<a href="{{ route('user.list') }}" class="btn btn-danger btn-sm" type="button" >
<span class="glyphicon glyphicon-remove"></span>
</a>
@endif
</span>
</div>
</div>
</div>
</div>
</div>
</form>
<div class="table-responsive" id="users-table-wrapper">
<table class="table table-bordered table-striped table-hover table-condensed" id="datatable-default">
<thead>
<th>@lang('app.full_name')</th>
<th>Sex<i type="button" class="fa fa-fw fa-sort"></th>
<th>Birthday<button name="birthday_sort" style="background-color:#ffffff"><i class="fa fa-fw fa-sort"></button></th>
<th>Age<i class="fa fa-fw fa-sort"></th>
<th>Civil<br>Status<i class="fa fa-fw fa-sort"></th>
<th>Company<i class="fa fa-fw fa-sort"></th>
<th>Department<i class="fa fa-fw fa-sort"></th>
<th>Employee<br>Status<i class="fa fa-fw fa-sort"></th>
<th>Level<i class="fa fa-fw fa-sort"></th>
<th>Date<br>Hired<i class="fa fa-fw fa-sort"></th>
<th>Tenure</th>
</thead>
<tbody>
@if (count($users))
@foreach ($users as $user)
<tr class="gradeX">
<td><a href="{{ route('user.show', $user->id) }}">{{ $user->first_name . ' ' . $user->middle_name . ' ' . $user->last_name }}</a></td>
<td>{{ $user->gender[0] }}</td>
<td>{{ $user->birthday->format('M j/y') }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->civil_status }}</td>
<td>@foreach ($user->company as $company)@endforeach{{ $company->name }}</td>
<td>@foreach ($user->department as $department)@endforeach{{ $department->name }}</td>
<td>{{ $user->emp_status }}</td>
<td>{{ $user->level }}</td>
<td>{{ $user->date_hired }}</td>
<td>{{ $user->difference }}</td>
</tr>
@endforeach
@else
<tr class="gradeX">
<td colspan="6"><em>@lang('app.no_records_found')</em></td>
</tr>
@endif
</tbody>
</table>
{!! $users->render() !!}
</div>
问题在于它说“全部”,“公司”等错误是未定义的变量。也因为这些公司是另一张桌子。如何在我的Excel中定义它不会再出错?
答案 0 :(得分:2)
您似乎需要将变量传递给要传递的控制器进行查看。请检查我的代码并更新。
public function userExport()
{
$companies = $this->companyModel->get();
$all = $this->something->get();
Excel::create('data_function',function($excel) use($all, $companies) {
$excel->sheet('mysheet', function($sheet) use($all, $companies) {
$sheet->loadView('user.list', ['all' => $all, 'companies' => $companies]);
});
})->download('xls');
}
并传递其他变量,与我在示例中传递给$sheet->loadView()
的变量相同。
我想这会对你有帮助。