分享
三行代码  ›  专栏  ›  技术社区  ›  anonymus

为什么在Laravel中保存数据时get method不允许出错?

  •  0
  • anonymus  · 技术社区  · 9 月前

    在我的应用程序中,我得到了尝试保存数据时不允许使用的错误方法。我在这里发布我的代码,请有人调查并帮助我。

    假日add.vue

    <template>
        <layout>
            <form @submit.prevent="handleSubmit">
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span for="name" class="input-group-text">First Name and Last Name </span>
                    </div>
                    <input type="text" class="form-control" name="firstname" placeholder="Enter your First Name" v-model="holiday.fname" id="fname">
                    <input type="text" class="form-control" name="lastname" placeholder="Enter your Last Name" v-model="holiday.lname" id="lname">
                </div>
                <br>
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text">Start Date </span>
                    </div>
                    <input type="date" class="form-control" name="startdate" v-model="holiday.sdate" id="sdate">
                </div>
                <br>
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text">End Date</span>
                    </div>
                    <input type="date" class="form-control" name="enddate" v-model="holiday.edate" id="edate">
                </div>
                <br>
                <button type="submit" class="btn btn-info">Apply</button>
            </form>
        </layout>
    </template>
    
    <script>
    import Layout from './../../Shared/Layout'
    export default {
        components: {
            Layout
        },
        data() {
            return {
                holiday: {
                    fname: '',
                    lname: '',
                    sdate: '',
                    edate: ''
                }
            }
        },
        methods: {
            async handleSubmit() {
                let response = await this.$inertia.post('/holiday/store', this.holiday)
            }
        }
    }
    </script>
    

    public function store(Request $request)
        {
    
            $holiday = $request->validate([
                'firstname' => 'required',
                'lastname' => 'required',
                'startdate' => 'required',
                'enddate' => 'required'
            ]);
    
            Holiday::create($holiday);
    
            return redirect()->route('holiday.index')->with('success', 'Record Inserted Successfully');
        }
    

    web.php网站

    Route::resource('holiday', 'HolidayController');
    

    据我所知,没有错误,那么为什么我会在这里得到405个错误呢?

    2 回复  |  直到 9 月前
        1
  •  1
  •   Nina Lisitsinskaya    9 月前

    在Vue代码中,应使用对 '/holiday' 而不是 '/holiday/store'

    定义资源路由相当于:

    Route::get('/holiday', 'HolidayController@index');
    Route::get('/holiday/create', 'HolidayController@create');
    Route::post('/holiday', 'HolidayController@store');
    Route::get('/holiday/{holiday}', 'HolidayController@show');
    Route::get('/holiday/{holiday}/edit', 'HolidayController@edit');
    Route::put('/holiday/{holiday}', 'HolidayController@update');
    Route::patch('/holiday/{holiday}', 'HolidayController@update');
    Route::delete('/holiday/{holiday}', 'HolidayController@destroy');
    

    https://laravel.com/docs/5.8/controllers#resource-controllers

        2
  •  1
  •   Foued MOUSSI    9 月前

    你的网址 '/holiday/store' Route::resource('holiday', 'HolidayController');

    修复

    await this.$inertia.post('holiday', this.holiday)
    

    运行以下命令

    php artisan route:list