此问题在此处已有答案:
Laravel resource route with name gives error(2个答案)
昨天关闭。
Too few arguments to function Illuminate\Routing\PendingResourceRegistration::name(),
1 passed in C:\xampp\htdocs\project\routes\web.php on line 18
and exactly 2 expected.
我尝试在laravel的Klorofil模板,但它的工作完美.我不知道为什么,但我没有打开laravel和php几次.当我再次打开这个项目,这是happenning.我刚刚想起也许我改变了路线或控制器,因为我想 AJAX withou刷新,和错误,而且我忘了像以前一样改变。但是当再次搜索这个模板的时候,也许不是我的错...或者php上有更新。
网页.php
Route::get('/', function () {
return view('main');
});
Route::resource('siswa', 'SiswaController')->name('siswa');
Route::get('/login','AuthController@login')->name('login');
Route::post('/postlogin', 'AuthController@postlogin');
Route::get('/logout','AuthController@logout');
Route::get('/dashboard', 'DashboardController@index')->middleware('auth');
Route::get('siswa.index', 'SiswaController@index')->middleware('auth');
Route::post('siswa.index/import', 'SiswaController@import')->middleware('auth');
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Siswa;
use DB;
use Excel;
class SiswaController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$siswas = siswa::all()->toarray();
return view('siswa.index', compact('siswas'));
$siswas = siswa::table('siswas')->orderBy('id', 'DESC')
->get();
return view('siswa.index', compact('siswas'));
}
public function import(Request $request)
{
$this->validate($request, [
'select_file' => 'required|mimes:xls,xlsx'
]);
$path = $request->file('select_file')->getRealPath();
$siswas = Excel::load($path)->get();
if ($siswas->count() > 0)
{
foreach ($siswas->toArray() as $key => $value)
{
foreach ($value as $row)
{
$insert_data[] = array(
'gambar' => $row['gambar'],
'name' => $row['name'],
'nisn' => $row['nisn'],
'kelas' => $row['kelas'],
'jurusan' => $row['jurusan'],
'jns_kel' => $row['jns_kel'],
'tanggal_lahir' => $row['tanggal_lahir'],
'alamat' => $row['alamat']
);
}
}
if (!empty($insert_data))
{
siswa::table('siswas')->insert($insert_data);
}
}
return back()->with('success', 'Data Excel Berhasil Di Tambah.');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('siswa.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'gambar' => 'required',
'name' => 'required',
'nisn' => 'required',
'kelas' => 'required',
'jurusan' => 'required',
'jns_kel' => 'required',
'tanggal_lahir' => 'required',
'alamat' => 'required'
]);
$siswa = new siswa([
'gambar' => $request->get('gambar'),
'name' => $request->get('name'),
'nisn' => $request->get('nisn'),
'kelas' => $request->get('kelas'),
'jurusan' => $request->get('jurusan'),
'jns_kel' => $request->get('jns_kel'),
'tanggal_lahir' => $request->get('tanggal_lahir'),
'alamat' => $request->get('alamat')
]);
$siswa->save();
return redirect()->route('siswa.index')->with('success', 'SUKSES TAMBAH DATA');
}
/**
* 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)
{
$siswa = siswa::find($id);
return view('siswa.ubah', compact('siswa', '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)
{
$this->validate($request, [
'gambar' => 'required',
'name' => 'required',
'nisn' => 'required',
'kelas' => 'required',
'jurusan' => 'required',
'jns_kel' => 'required',
'tanggal_lahir' => 'required',
'alamat' => 'required'
]);
$siswa = siswa::find($id);
$siswa->gambar = $request->get('gambar');
$siswa->name = $request->get('name');
$siswa->nisn = $request->get('nisn');
$siswa->kelas = $request->get('kelas');
$siswa->jurusan = $request->get('jurusan');
$siswa->jns_kel = $request->get('jns_kel');
$siswa->tanggal_lahir = $request->get('tanggal_lahir');
$siswa->alamat = $request->get('alamat');
$siswa->save();
return redirect()->route('siswa.index')->with('success', 'DATA TERBARUI');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$siswa = siswa::find($id);
$siswa->delete();
return redirect()->route('siswa.index')->with('success', 'DATA SUDAH DI HAPUS');
}
}
查看文件夹:西瓦指数
@extends('main')
@section('content')
<div class="main">
<div class="main-content">
<div class="container-fluid">
<div class="row">
<div class="col-md-10" style="margin: 70px 240px" >
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title">Data Siswa</h3>
<div align="right">
<!-- Button trigger modal -->
<button type="button" class="btn" data-toggle="modal" data-target="#exampleModal">
<i class="inr-plus-circle">Tambah Data Siswa</i>
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel" >Tambah Data Siswa</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" action="{{url('siswa')}}">
{{csrf_field()}}
<div class="form-group">
<input type="text" name="gambar" class="form-control" placeholder="Berkas Gambar">
</div>
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Masukkan Nama">
</div>
<div class="form-group">
<input type="text" name="nisn" class="form-control" placeholder="Masukkan nisn">
</div>
<div class="form-group" class="form-control"><p>Masukkan Kelas :</p>
<select name="kelas" class="bg-primary text-black">
<option>X</option>
<option>XI</option>
<option>XII</option>
</select>
</div>
<div class="form-group">
<input type="text" name="jurusan" class="form-control" placeholder="Jurusan Mu Apa Dong">
</div>
<div class="form-group" class="form-control"><p>Jenis Kelamin :</p>
<select name="jns_kel" class="bg-primary text-black">
<option>Cowok</option>
<option>Cewek</option>
</select>
</div>
<div class="form-group">
<input type="text" name="tanggal lahir" class="form-control" placeholder="Tanggal Lahir">
</div>
<div class="form-group">
<input type="text" name="alamat" class="form-control" placeholder="Jalan Rumah Mu">
</div>
<!-- <div class="form-group">
<button type="submit" class="btn btn-primary" style="color: black">Kirim</button>
</div> -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" style="background-color: cyan">Close</button>
<button type="submit" class="btn btn-primary" style="background-color: cyan">Submit</button>
</form>
</div>
</div>
</div>
</div>
<br>
<br>
</div>
</div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Gambar</th>
<th>Name</th>
<th>Nisn</th>
<th>Kelas</th>
<th>Jurusan</th>
<th>Jenis Kelamin</th>
<th>Tanggal Lahir</th>
<th>Alamat</th>
<th>Ubah</th>
<th>Hapus</th>
</tr>
</thead>
<tbody>
@foreach($siswas as $row)
<tr>
<td><img src="<?= $row["gambar"]; ?>" width="50"></td>
<td>{{$row['name']}}</td>
<td>{{$row['nisn']}}</td>
<td>{{$row['kelas']}}</td>
<td>{{$row['jurusan']}}</td>
<td>{{$row['jns_kel']}}</td>
<td>{{$row['tanggal_lahir']}}</td>
<td>{{$row['alamat']}}</td>
<td><button class="btn btn-warning"><a href="{{action('SiswaController@edit', $row['id'])}}" style="color: black">UBAH</a></button></td>
<td>
<form method="post" class="delete_form" action="{{action('SiswaController@destroy', $row['id'])}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-danger">HAPUS</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@stop
@section('content1')
<div class="row">
<div class="col-md-12">
<br>
<h3 align="center">Data Siswa</h3>
<br>
@if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
@endif
<!-- tombol menambah data baru manual -->
<div align="right">
<a href="{{route('siswa.create')}}" class="btn btn-primary">Tambah</a>
<br>
<br>
</div>
<!-- akhir tombol tambah -->
<!-- menambah choose file excel -->
@if(count($errors) > 0)
<div class="alert alert-danger">
Upload Validation Error<br><br>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" enctype="multipart/form-data"
action="{{ url('/siswa.index/import') }}">
{{ csrf_field() }}
<div class="form-group">
<table class="table">
<tr>
<td width="40%" align="right"><label>Pilih Untuk Upload File</label></td>
<td width="30">
<input type="file" name="select_file">
</td>
<td width="30%" align="left">
<input type="submit" name="upload" class="btn btn-primary" value="upload">
</td>
</tr>
<tr>
<td width="40%" align="right"></td>
<td width="30"><span class="text-muted">.xls, .xslx</span></td>
<td width="30%" align="left"></td>
</tr>
</table>
</div>
</form>
<!-- akhir menambah choose file excel -->
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>Gambar</th>
<th>Name</th>
<th>Nisn</th>
<th>Kelas</th>
<th>Jurusan</th>
<th>Jenis Kelamin</th>
<th>Tanggal Lahir</th>
<th>Alamat</th>
<th>Ubah</th>
<th>Hapus</th>
</tr>
@foreach($siswas as $row)
<tr>
<td><img src="<?= $row["gambar"]; ?>" width="50"></td>
<td>{{$row['name']}}</td>
<td>{{$row['nisn']}}</td>
<td>{{$row['kelas']}}</td>
<td>{{$row['jurusan']}}</td>
<td>{{$row['jns_kel']}}</td>
<td>{{$row['tanggal_lahir']}}</td>
<td>{{$row['alamat']}}</td>
<td><button class="btn btn-warning"><a href="{{action('SiswaController@edit', $row['id'])}}" style="color: black">UBAH</a></button></td>
<td>
<form method="post" class="delete_form" action="{{action('SiswaController@destroy', $row['id'])}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-danger">HAPUS</button>
</form>
</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
@endsection
查看文件夹:主文件.blade.php
<!doctype html>
<html lang="en">
<head>
<title>Dashboard | Klorofil - Free Bootstrap Dashboard Template</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<!-- VENDOR CSS -->
<link rel="stylesheet" href="{{asset('admin/assets/vendor/bootstrap/css/bootstrap.min.css')}}">
<link rel="stylesheet" href="{{asset('admin/assets/vendor/font-awesome/css/font-awesome.min.css')}}">
<link rel="stylesheet" href="{{asset('admin/assets/vendor/linearicons/style.css')}}">
<link rel="stylesheet" href="{{asset('admin/assets/vendor/chartist/css/chartist-custom.css')}}">
<!-- MAIN CSS -->
<link rel="stylesheet" href="{{asset('admin/assets/css/main.css')}}">
<!-- FOR DEMO PURPOSES ONLY. You should remove this in your project -->
<link rel="stylesheet" href="{{asset('admin/assets/css/demo.css')}}">
<!-- GOOGLE FONTS -->
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700" rel="stylesheet">
<!-- ICONS -->
<link rel="apple-touch-icon" sizes="76x76" href="{{asset('admin/assets/img/apple-icon.png')}}">
<link rel="icon" type="image/png" sizes="96x96" href="{{asset('admin/assets/img/favicon.png')}}">
</head>
<body>
<!-- WRAPPER -->
<div id="wrapper">
<!-- NAVBAR -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="brand">
<a href="index.html"><img src="{{asset('admin/assets/img/logo-dark.png')}}" alt="Klorofil Logo" class="img-responsive logo"></a>
</div>
<div class="container-fluid">
<div class="navbar-btn">
<button type="button" class="btn-toggle-fullwidth"><i class="lnr lnr-arrow-left-circle"></i></button>
</div>
<form class="navbar-form navbar-left">
<div class="input-group">
<input type="text" value="" class="form-control" placeholder="Search Data Siswa...">
<span class="input-group-btn"><button type="button" class="btn btn-primary">Go</button></span>
</div>
</form>
<div id="navbar-menu">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle icon-menu" data-toggle="dropdown">
<i class="lnr lnr-alarm"></i>
<span class="badge bg-danger">5</span>
</a>
<ul class="dropdown-menu notifications">
<li><a href="#" class="notification-item"><span class="dot bg-warning"></span>System space is almost full</a></li>
<li><a href="#" class="notification-item"><span class="dot bg-danger"></span>You have 9 unfinished tasks</a></li>
<li><a href="#" class="notification-item"><span class="dot bg-success"></span>Monthly report is available</a></li>
<li><a href="#" class="notification-item"><span class="dot bg-warning"></span>Weekly meeting in 1 hour</a></li>
<li><a href="#" class="notification-item"><span class="dot bg-success"></span>Your request has been approved</a></li>
<li><a href="#" class="more">See all notifications</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="lnr lnr-question-circle"></i> <span>Help</span> <i class="icon-submenu lnr lnr-chevron-down"></i></a>
<ul class="dropdown-menu">
<li><a href="#">Basic Use</a></li>
<li><a href="#">Working With Data</a></li>
<li><a href="#">Security</a></li>
<li><a href="#">Troubleshooting</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="{{asset('admin/baik.jpg')}}" class="img-circle" alt="Avatar"> <span>{{auth()->user()->name}}</span> <i class="icon-submenu lnr lnr-chevron-down"></i></a>
<ul class="dropdown-menu">
<li><a href="#"><i class="lnr lnr-user"></i> <span>My Profile</span></a></li>
<li><a href="#"><i class="lnr lnr-envelope"></i> <span>Message</span></a></li>
<li><a href="#"><i class="lnr lnr-cog"></i> <span>Settings</span></a></li>
<li><a href="/logout"><i class="lnr lnr-exit"></i> <span>Logout</span></a></li>
</ul>
</li>
<!-- <li>
<a class="update-pro" href="https://www.themeineed.com/downloads/klorofil-pro-bootstrap-admin-dashboard-template/?utm_source=klorofil&utm_medium=template&utm_campaign=KlorofilPro" title="Upgrade to Pro" target="_blank"><i class="fa fa-rocket"></i> <span>UPGRADE TO PRO</span></a>
</li> -->
</ul>
</div>
</div>
</nav>
<!-- END NAVBAR -->
<!-- LEFT SIDEBAR -->
<div id="sidebar-nav" class="sidebar">
<div class="sidebar-scroll">
<nav>
<ul class="nav">
<li><a href="/" class=""><i class="lnr lnr-home"></i> <span>Dashboard</span></a></li>
<li><a href="siswa" class="active"><i class="lnr lnr-user"></i> <span>Siswa</span></a></li>
<!-- <li><a href="charts.html" class=""><i class="lnr lnr-chart-bars"></i> <span>Charts</span></a></li>
<li><a href="panels.html" class=""><i class="lnr lnr-cog"></i> <span>Panels</span></a></li>
<li><a href="notifications.html" class=""><i class="lnr lnr-alarm"></i> <span>Notifications</span></a></li>
<li>
<a href="#subPages" data-toggle="collapse" class="collapsed"><i class="lnr lnr-file-empty"></i> <span>Pages</span> <i class="icon-submenu lnr lnr-chevron-left"></i></a>
<div id="subPages" class="collapse ">
<ul class="nav">
<li><a href="page-profile.html" class="">Profile</a></li>
<li><a href="page-login.html" class="">Login</a></li>
<li><a href="page-lockscreen.html" class="">Lockscreen</a></li>
</ul> -->
</div>
</li>
<!-- <li><a href="tables.html" class=""><i class="lnr lnr-dice"></i> <span>Tables</span></a></li>
<li><a href="typography.html" class=""><i class="lnr lnr-text-format"></i> <span>Typography</span></a></li>
<li><a href="icons.html" class=""><i class="lnr lnr-linearicons"></i> <span>Icons</span></a></li> -->
</ul>
</nav>
</div>
</div>
<!-- END LEFT SIDEBAR -->
<!-- MAIN -->
@yield('content')
<!-- END MAIN -->
<div class="clearfix"></div>
<footer>
<div class="container-fluid">
<p class="copyright">Shared by <i class="fa fa-love"></i><a href="https://bootstrapthemes.co">BootstrapThemes</a>
</p>
</div>
</footer>
</div>
<!-- END WRAPPER -->
<!-- Javascript -->
<script src="http://code.jquery.com/jquery-3.4.0.js" integrity="sha256-DYZMCC8HTC+QDr5QNaIcfR7VSPtcISykd+6eSmBW5qo=" crossorigin="anonymous"></script>
<script src="{{asset('admin/assets/vendor/jquery/jquery.min.js')}}"></script>
<script src="{{asset('admin/assets/vendor/bootstrap/js/bootstrap.min.js')}}"></script>
<script src="{{asset('admin/assets/vendor/jquery-slimscroll/jquery.slimscroll.min.js')}}"></script>
<script src="{{asset('admin/assets/vendor/jquery.easy-pie-chart/jquery.easypiechart.min.js')}}"></script>
<script src="{{asset('admin/assets/vendor/chartist/js/chartist.min.js')}}"></script>
<script src="{{asset('admin/assets/scripts/klorofil-common.js')}}"></script>
</body>
</html>
结果必须给予我一个表一些人数据
4条答案
按热度按时间8ljdwjyq1#
解决方法是将路线更改为:
这可能来得很晚,但我刚刚遇到了同样的错误。发生错误是因为当我们使用
Route::resource
时,Laravel需要2个参数而不是1个。您只定义了名称。因此,Laravel为您定义了路线“siswa”。
但是因为你使用的是
Route::resource
,所以里面定义了很多路由,比如create、edit、delete等等,所以Laravel需要定义路由为'siswa. create'、'siswa. delete'等等,当然不能期望我们列出所有的方法,所以在上面加上一个 * 可以帮助Laravel理解它应该把命名的路由创建为siswa.*,并且 * 代表所有不同的方法(例如,创建、编辑、删除等等)。希望这能有所帮助!5n0oy7gb2#
路线:
是导致所有问题的原因。您在顶部声明了一个路由资源,然后又声明了另一个名为
siswa.index
的路由,由于线路Route::resource('siswa', 'SiswaController')->name('siswa');
,该路由已经存在。创建资源将生成如下路由:因此,如果一个路由名称被声明两次,它将抛出一个缺少参数的错误。
oyxsuwqo3#
只需从web.php的第18行删除**-〉name('siswa')**即可。
并使用路由名称到刀片文件i.g:获取索引页
{{ route('siswa.index') }}
,创建表单{{ route('siswa.create') }}
ccrfmcuu4#
我今天也收到了一个相同的。我找到了一个结果当我使用route:list时,我得到了结果
| 获取|头部|管理员/用户| user.index |应用程序\Http\控制器\管理员用户控制器@index|腹板|
| - ------|- ------|- ------|- ------|- ------|- ------|
| | 获取|头部|管理员/用户/创建| user.create |应用程序\Http\控制器\管理员用户控制器@create|
我已更改路线:如下
路由::资源('/admin/用户','管理员用户控制器',['as' =〉'管理员']);
然后
我在Route:list上找到了结果
| 获取|头部|管理员/用户| admin.user.index |应用程序\Http\控制器\管理员用户控制器@index|腹板|
| - ------|- ------|- ------|- ------|- ------|- ------|
| | 获取|头部|管理员/用户/创建| admin.user.create |应用程序\Http\控制器\管理员用户控制器@create|
因此,我建议您按如下方式更改路线文件:
路由::资源('/admin/用户','管理员用户控制器',['as' =〉'管理员']);