如何在Laravel 5.7和Laravel Collective中使用动作

时间:2018-10-23 01:27:57

标签: php laravel

每次我要在表单中使用Action()时都会遇到此错误

Action App\Http\Controllers\RentBikeController@store not defined. (View: C:\xampp\htdocs\BikeRental\resources\views\Pages\bike.blade.php)

我的控制器代码是:

<?php
     namespace App\Http\Controllers;

     use Illuminate\Http\Request;
     use App\Http\Requests;

     class RentBikeController extends Controller
     {
     /**
      * Display a listing of the resource.
      *
      * @return \Illuminate\Http\Response
      */
     public function index()
     {
         //
     }

     /**
      * Show the form for creating a new resource.
      *
      * @return \Illuminate\Http\Response
      */
     public function create()
     {
         //
     }

     /**
      * Store a newly created resource in storage.
      *
      * @param  \Illuminate\Http\Request  $request
      * @return \Illuminate\Http\Response
      */
     public function store(Request $request)
     {


         echo 'chuj';
     }

     /**
      * 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)
     {
         //
     }

     /**
      * Update the specified resource in storage.
      *
      * @param  \Illuminate\Http\Request  $request
      * @param  int  $id
      * @return \Illuminate\Http\Response
      */
     public function update(Request $request, $id)
     {
         //
     }

     /**
      * Remove the specified resource from storage.
      *
      * @param  int  $id
      * @return \Illuminate\Http\Response
      */
     public function destroy($id)
     {
         //
     }
   }

查看代码

{!! Form::open(['action' => 'RentBikeController@store']) !!} 
     <div class="form-group">
         {{Form::label('title', 'Title')}}
         {{Form::text('title', '', ['class' => 'form-conrol', 'placeholder'=> 'Title'])}}
     </div>
     <div class="form-group">
         {{Form::label('body', 'Body')}}
         {{Form::textarea('body', '', ['class' => 'form-conrol', 'placeholder'=> 'text'])}}
     </div>
    {{Form::submit('submit', ['class' => 'btn btn-primary'])}}
{!! Form::close() !!} 

我知道我可以使用类似Route::post('/bike/1','BikeController@store')的东西,但是我真的不想要。 我也尝试使用此解决方案Adding form action in html in laravel,但随后出现另一个错误

1 个答案:

答案 0 :(得分:2)

将此添加到路由文件夹 web.php

Route::resource('bikes', 'RentBikeController');

然后将您的Form::open更改为此:

{!! Form::open(['method' => 'POST', 'route' => ['bikes.store'], 'autocomplete' =>
                            'off', 'enctype' => 'multipart/form-data']) !!}