【问题标题】:Laravel 5.3 : Method [show] does not existLaravel 5.3:方法[显示]不存在
【发布时间】:2017-12-08 07:26:04
【问题描述】:

将我的表单发布到控制器时,laravel 显示此错误:

方法 PTA_OIMS\Http\Controllers\InstructionsController::show() 确实 不存在于 /home/rad/public_html/pta_oims/vendor/laravel/framework/src/Illuminate/Routing/Route.php:333

这是我的 web.php 文件:

Route::post('instructions/view','InstructionsController@view')->name('view');
Route::post('instructions/assign','InstructionsController@assign');
Route::get('instructions/sent','InstructionsController@sent')->name('sent');
Route::post('instructions/reject','InstructionsController@reject')->name('reject');
Route::resource('instructions','InstructionsController');

和刀片视图:

 <form action=" {{url('instructions/assign')}}"

变得更加完整:

InstructionsController 并添加了 show()、view() 和 assign() 方法

public function show()
{
    return 'show() method done!!';
}

view() 方法

public function view(Request $request)
{
    $boxes = $this->getBoxesList();
    $id = $request->input('id');
    $data = Kartable::where('id', '=', $id)
        ->with('instruction')
        ->first();
    if ($data->view_date == NULL) {
        $this->insertViewDate($id);
    }

    return view('instructions.view', array('data' => $data, 'boxes' => $boxes));
}

查看刀片文件,将数据发送到assign()方法的表单:

<form method="POST" action=" {{url('instructions/assign')}}" 
    class="form-horizontal" role="form" data-toggle="validator">

    {{csrf_field()}}

assign() 方法

public function assign(Request $input)
{
    try {

        $inst_id = $input->input('inst_id');
//        $childsInsts = Instructions::where('parent_id', '=', $inst_id)
//             ->where('status', '=', 0)
//             ->get();
        $formData = $input->all();
        $step = 0;
        foreach ($formData['box'] as $box) {
            foreach ($box as $id_reciver)
                DB::transaction(function () use ($formData, $inst_id, $id_reciver, $step) {
                    $kar = new Kartable();

                    $kartable_id = $formData['kartable_id'];
                    $current_box = $formData['current_box'];
                    $kar->inst_id = $inst_id;
                    $kar->parent_id = $kartable_id;
                    $kar->id_sender = $current_box;
                    $kar->id_reciver = $id_reciver;
                    $kar->status = 1;
                    $kar->save(); // insert for new record of kartable
                    $step += 1;
                    if ($step < 2) {
                        $kartable = new Kartable();
                        $kartable
                            ->where('id', '=', $kartable_id)
                            ->update(['status' => 3]); //change status of old row kartable
                    }
                });

            return redirect()->route('instructions.index');
        }
    } catch (\Exception $e) {
        dd($e);
    }
}

【问题讨论】:

    标签: laravel controller routes


    【解决方案1】:

    你检查过你的“InstructionsController”有一个“show”方法吗?

    因为您要声明 Route::resource('instructions','InstructionsController'); 然后你 Laravel 假设控制器有以下方法:

    查看this docs from Laravel bro.

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      我想这是因为您的&lt;form&gt; 标签上没有method=,这将导致表单与GET 请求一起提交,并看到您设置了Route::resource将假定您正在尝试访问 show 方法。

      将您的表单标签更改为:

      <form method="POST" action="{{ url('instructions/assign') }}">
      

      另外,别忘了补充:

      {{ csrf_field() }}
      

      在您的表单中,这样您就不会收到TokenMismatchException

      希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        需要定义你的路由器。

        Route::get('instructions/show','InstructionsController@show')->name('show');
        

        【讨论】:

          猜你喜欢
          • 2014-05-21
          • 2017-05-30
          • 2017-06-18
          • 2017-06-12
          • 1970-01-01
          • 2017-08-18
          • 2017-03-10
          • 2017-06-21
          • 2017-04-11
          相关资源
          最近更新 更多