1

I can't create new database record, because after submit it always shows POST 405 (not allowed). I tried to move laravel routes from api.php to web.php but it didn't work. Also tried to modificate axios.post link (create, /create, localhost://Reservationsystem/public/create) but it also didn't gave me result.

Web.php

Route::resource('/reservation','ReservationController');
Route::get('/{any}', 'SinglePageController@index')->where('any', '.*');

Routes.js

import Vue from 'vue'
import Router from 'vue-router'
import Home from './components/HomeComponent.vue'
import Reservations from './components/ReservationsComponent.vue'

Vue.use(Router);

export default new Router({
    routes: [
        {
            path: '/',
            name: 'home',
            component: Home
        },
        {
            path: '/reservations',
            name: 'reservations',
            component: Reservations
        }
    ]
})

HomeComponent.vue

<script>
    import axios from 'axios';

    export default {
        data: function () {
            return {
                reservation: {
                    name: '',
                    surname: '',
                    email: '',
                    date: '',
                    place: ''
                }
            }
        },
        methods: {
            addReservation: function () {
                let uri = 'reservation';
                axios.post(uri, this.reservation).then(res => this.reservation = [...this.reservation, res.data]);
            }
        }
    }
</script>

controller method:

public function store(Request $request)
{
    $reservation= new Reservation($request->all());
    $reservation->save();

    return new ReservationResource($reservation);
}
5
  • Please include code in the controller. Do you have a public function store? Commented Feb 8, 2020 at 12:51
  • Yes. Method is added now Commented Feb 8, 2020 at 12:54
  • Try leaving the route Route::resource('/reservation','ReservationController'); in api.php and make the axios call to /api/reservation url. Commented Feb 8, 2020 at 13:20
  • Please check you network tab when making the call. What is the full url the call is going to (in the network tab)? Commented Feb 8, 2020 at 13:24
  • When I tried to access post method url it shows me blank page, it was strange and after some researches I found that after public in url I need to add index.php (localhost/project/public/index.php/...) it seems to be server configuration problem.. Commented Feb 8, 2020 at 13:34

2 Answers 2

2

The reason you may be getting 405 is because the route is not Post to coincide with post in the addReservation method.I would use the following for the post route:

Route::post('/reservation/store','ReservationController@store');

for the uri in axios I would use:

let uri = 'reservation/store';

Here is a link to a post showing how I used axios with Laravel. Blog Post With Code

Sign up to request clarification or add additional context in comments.

Comments

0

Yo you use https? If yes, do not use / in end of address.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.