Laravel 5.3:方法[show]不存在

时间:2017-07-04 18:49:05

标签: laravel controller routes

将表单发布到控制器时,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);
    }
}

3 个答案:

答案 0 :(得分:0)

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

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

enter image description here

检查this docs from Laravel bro.

希望对你有所帮助。

答案 1 :(得分:0)

我想这可能是因为您的method=标记上没有<form>,这会导致表单通过GET请求提交,并视为您如果设置Route::resource,则会假设您正在尝试访问show方法。

将表单标记更改为:

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

另外,不要忘记添加:

{{ csrf_field() }}

在您的表单中,以便您不会获得TokenMismatchException

希望这有帮助!

答案 2 :(得分:0)

需要定义路由器。

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