如何解决无法在 Laravel 中导出 Excel,找不到 404
我在 Laravel 中导出 Excel 文件时遇到了一些问题。当我单击导出按钮时,页面显示“404 Not Found”。我试过 php artisan route:clear
或 php artisan route:cache
但它不起作用。
希望你能帮我解决问题。这是我的代码:
App\Teacher.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Teacher extends Model {
protected $guarded = [];
protected $table = 'teachers';
}
App\Exports\TeacherExport.php:
<?php
namespace App\Exports;
use App\Teacher;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class TeacherExport implements FromCollection,WithHeadings {
public function headings():array {
return [
'id','name','email','created_at','updated_at'
];
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection() {
return Teacher::all();
}
}
App\Controllers\TeachersController.php:
public function exportTeacherIntoExcel() {
return Excel::download(new TeacherExport,'teacher.xlsx');
}
web.php:
Route::get('/teacher/export-excel','TeachersController@exportTeacherIntoExcel');
资源\视图\教师\index.blade.php:
@extends('home')
@section('content')
<div class="card mb-3">
<div class="card-body">
<h5 class="card-title">
Danh sách giáo viên
</h5>
<p class="card-text">
Bạn có thể tìm tất cả thông tin giáo viên ở đây:
</p>
</div>
</div>
<a href="/teacher/create" class="btn btn-info">
Thêm giáo viên
</a>
<a href="/teacher/export-excel" class="btn btn-success">
Xuất file
</a>
<table class="table thead-light">
<thead>
<tr>
<th scope="col">
ID
</th>
<th scope="col">
Tên giáo viên
</th>
<th scope="col">
Địa chỉ Mail
</th>
<th scope="col">
Tính năng
</th>
<th scope="col">
Tính năng
</th>
</tr>
</thead>
<tbody>
@foreach($teacher as $teacher)
<tr>
<th scope="row">
{{$teacher->id}}
</th>
<td>
{{$teacher->name}}
</td>
<td>
{{$teacher->email}}
</td>
<!-- <td>Tính năng</td> -->
<td>
<a href="/teacher/{{$teacher->id}}/edit" class = "btn btn-success">
Sửa
</a>
</td>
<td>
<a href="/teacher/{{$teacher->id}}" class="btn btn-danger">
Xóa
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
解决方法
谢谢大家。我已经解决了我的问题。这里的问题是路线的顺序。在我的路线中,我像这样修复了它并且它有效。
web.php:
/**
* Teacher
*/
Route::get('/teacher/export-excel','TeachersController@exportTeacherIntoExcel');
Route::get('/teacher','TeachersController@index');
Route::get('/teacher/create','TeachersController@create');
Route::post('/teacher','TeachersController@store');
Route::get('/teacher/{teacher}/edit','TeachersController@edit');
Route::patch('/teacher/{teacher}','TeachersController@update');
Route::get('/teacher/{teacher}','TeachersController@destroy');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。