【问题标题】:Missing required parameters for [Route: emps.show] [URI: emps/{emp}]. (View: D:\xampp\htdocs\crud1\resources\views\emps\index.blade.php)[Route: emps.show] [URI: emps/{emp}] 缺少必需的参数。 (查看:D:\xampp\htdocs\crud1\resources\views\emps\index.blade.php)
【发布时间】:2023-03-10 20:53:01
【问题描述】:

这是我的 empcontroller.blade.php

     * Display the specified resource.
     *
     * @param  \App\employee  $employee
     * @return \Illuminate\Http\Response
     */
    public function show(employee $employee)
    {
        $employees = employee::all();

        return view ('emps.show',compact('employees'))->with('i');

    }

这是我的 show.blade.php

@extends('emps.layout')

@section('content')
<div class="row">
    <div class="col-sm-12 col-md-12 col-xs-12">
            <div class="pull-right">
               <a href="{{route('emps.index')}}">Back</a>
            </div>
    </div>
    <table class="table table-borderd table-hover">
        <tr>
            <th>Name:</th>
            <th>Age:</th>
            <th>Mobile no:</th>
            <th>Departmetn:</th>
            <th>State:</th>
        </tr>
        @foreach ($employees as $employee)
    <tr>
        <td>{{++$i}}</td>
        <td>{{$employee->name}} </td>
        <td>{{$employee->age}}</td>
        <td>{{$employee->mobile}} </td>
        <td>{{$employee->department}}</td>
        <td>{{$employee->state}}</td>
     </tr>
        @endforeach
    </table>
    </div>

</div>
@endsection

这是我的 route.web

Route::resource('emps', 'empcontroller');

这是我的 index.blade.php

<div class="pull-left">
<a href="{{route('emps.create')}}">Add New product</a>
<a href="{{route('emps.show')}}">show employees</a>
</div>

当我点击显示员工时抛出此错误 [Route: emps.show] [URI: emps/{emp}] 缺少必需的参数。 (查看:D:\xampp\htdocs\crud1\resources\views\emps\index.blade.php)

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    问题出在这行代码:

    <a href="{{route('emps.show')}}">show employees</a>
    

    您必须传递一个员工的 id 参数才能仅显示该员工。

    像这样:

    <a href="{{route('emps.show', ['id'=>$someId])}}">show employees</a>
    

    还有一件事,在show方法中你得到所有员工然后返回他们,这不是show方法应该做的,它是返回所有员工的index方法show方法只显示一条记录

    另一件事是关于命名约定,类名应该以大写Employee开头

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2019-08-29
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多