在 Laravel excel 中导入时未定义路由
Route undefined when importing in Laravel excel
我正在尝试使用 Laravel excel 的 import
功能,但它总是 return Route: undefined error
即使路由已注册。请查看我的代码和下面的详细信息。
web.php
Route::get('admin/doctors/bulk-upload/data', [
'middleware' => ['auth', 'auth.admin'],
'as' => 'admin.doctors.bulk-upload.data',
'uses' => 'Admin\DoctorUploadController@create'
]);
Route::post('admin/doctors/bulk-data/upload', [
'as' => 'admin.doctors.bulk-data.upload',
'uses' => 'Admin\DoctorUploadController@upload'
]);
UsersImport.php
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow//, WithValidation
{
public function model(array $row)
{
$user = User::create([
'name' => $row['first_name'] . ' ' . $row['last_name'],
'email' => $row['email'],
'password' => bcrypt($row['email']),
'role' => 'doctor',
'status' => 1
]);
return $user;
}
public function headingRow()
{
return 1;
}
}
控制器
use App\Imports\UsersImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
class DoctorUploadController extends Controller
{
public function create()
{
return view('admin.doctors.bulk-upload');
}
public function upload()
{
$this->validate(request(), [
'excel' => ['required', 'file', 'mimes:xlsx']
]);
Excel::import(new UsersImport, request()->file('excel'));
return redirect()->route('doctors.bulk-upload.data')->with('message', 'Data has been uploaded!');
}
}
查看
<form action="{{ route('admin.doctors.bulk-data.upload') }}" method="post" enctype="multipart/form-data" id="formUpload">
{{ csrf_field() }}
<div class="mb-3">
<input type="file" class="pointer" name="excel" accept=".xlsx">
</div>
<button class="btn btn-primary btn-block">
<span class="fas fa-file-upload"></span> Upload
</button>
</form>
错误
Route [doctors.bulk-upload.data] not defined.
您在重定向路由 doctors.bulk-upload.data
到 admin.doctors.bulk-upload.data
时出错
return redirect()->route('admin.doctors.bulk-upload.data')->with('message', 'Data has been uploaded!');
我正在尝试使用 Laravel excel 的 import
功能,但它总是 return Route: undefined error
即使路由已注册。请查看我的代码和下面的详细信息。
web.php
Route::get('admin/doctors/bulk-upload/data', [
'middleware' => ['auth', 'auth.admin'],
'as' => 'admin.doctors.bulk-upload.data',
'uses' => 'Admin\DoctorUploadController@create'
]);
Route::post('admin/doctors/bulk-data/upload', [
'as' => 'admin.doctors.bulk-data.upload',
'uses' => 'Admin\DoctorUploadController@upload'
]);
UsersImport.php
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow//, WithValidation
{
public function model(array $row)
{
$user = User::create([
'name' => $row['first_name'] . ' ' . $row['last_name'],
'email' => $row['email'],
'password' => bcrypt($row['email']),
'role' => 'doctor',
'status' => 1
]);
return $user;
}
public function headingRow()
{
return 1;
}
}
控制器
use App\Imports\UsersImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
class DoctorUploadController extends Controller
{
public function create()
{
return view('admin.doctors.bulk-upload');
}
public function upload()
{
$this->validate(request(), [
'excel' => ['required', 'file', 'mimes:xlsx']
]);
Excel::import(new UsersImport, request()->file('excel'));
return redirect()->route('doctors.bulk-upload.data')->with('message', 'Data has been uploaded!');
}
}
查看
<form action="{{ route('admin.doctors.bulk-data.upload') }}" method="post" enctype="multipart/form-data" id="formUpload">
{{ csrf_field() }}
<div class="mb-3">
<input type="file" class="pointer" name="excel" accept=".xlsx">
</div>
<button class="btn btn-primary btn-block">
<span class="fas fa-file-upload"></span> Upload
</button>
</form>
错误
Route [doctors.bulk-upload.data] not defined.
您在重定向路由 doctors.bulk-upload.data
到 admin.doctors.bulk-upload.data
return redirect()->route('admin.doctors.bulk-upload.data')->with('message', 'Data has been uploaded!');