Laravel routes going to wrong route

I do not understand the following problem.

Here are me routes:

Route::get('events', array('as' => 'events' ,'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2e7d4c7ccd6e1cdccd6d0cdcecec7d0e2cbccc6c7da">[email protected]</a>'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="733605161d07301c1d07011c1f1f160133051a1604">[email protected]</a>'));
Route::get('event/new_event', array('as'=> 'new_event', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9dd8ebf8f3e9def2f3e9eff2f1f1f8efddf3f8ead8ebf8f3e9">[email protected]</a>'));
Route::post('event/create', array('uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ecbf8ebe0facde1e0fafce1e2e2ebfcceedfcebeffaeb">[email protected]</a>'));
Route::get('event/{id}/edit', array('as' => 'edit_event', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e0b382b203a0d21203a3c2122222b3c0e2b2a273a">[email protected]</a>'));
Route::post('event/update', array('uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91d4e7f4ffe5d2feffe5e3fefdfdf4e3d1e4e1f5f0e5f4">[email protected]</a>'));
Route::delete('event/delete', array('uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b5e6d7e756f5874756f697477777e695b7f7e686f697462">[email protected]</a>'));

I can not create a new event, because when I click on the ‘New Event’ button, it uses [email protected] instead of [email protected].

Here is the EventController:

<?php

namespace AppHttpControllers;

use IlluminateSupportFacadesInput;
use IlluminateHttpRequest;
use AppEventModel;

class EventController extends Controller
{
    public function index()
    {
        $events = EventModel::all();
        return View::make('event/index')->with('events', $events);
    }

    public function view($id)
    {
        return View::make('event/view')
        ->with('event', EventModel::find($id));

    }

    public function newEvent()
    {
        dd("dd");
      return View::make('event/create');
    }

    public function create()
    {
        $validator = EventModel::validate(Input::all());

        if($validator->fails())
        {   
            $messages = $validator->messages();

            return redirect()->action('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="307546555e44735f5e44425f5c5c5542705e55477546555e44">[email protected]</a>')
            ->withErrors($validator)
            ->withinput();
        }
        else
        {
            EventModel::create(array(
                'title'=>Input::get('title'),
                'start'=>Input::get('start'),
                'end'=>Input::get('end'),
                'userID'=>Auth::user()->id,
            ));

            //Session::flash('message', 'New event has been created!');   
            flash()->overlay('New event has been created!', 'Success');

            return redirect()->back();
        }
    }

    public function edit($id)
    {
        return View::make('event/edit')
        ->with('event', EventModel::find($id));
    }

    public function update()
    {   
        $event = EventModel::find(Input::get('event_id'));

        $validator = EventModel::validate(Input::all());

        if($validator->fails())
        {   
            $messages = $validator->messages();

            return redirect()->back()
            ->withErrors($validator)
            ->withinput();
        }
        else
        {
            $event->title = Input::get('title');
            $event->start = Input::get('start');         
            $event->end = Input::get('end');
            $event->save();

            //Session::flash('message', 'Successfully updated!');     
            flash()->overlay('Event has been sucessfully updated!', 'Success');

            return redirect()->back();
        }
    }

    public function destroy()
    {
        $id = Input::get('event_id');
        dd("$id");
    }
}

Why does this problem occur?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have to sort your routes because laravel checks the order of the routes.

Try:

Route::get('events', array('as' => 'events' ,'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82c7f4e7ecf6c1edecf6f0edeeeee7f0c2ebece6e7fa">[email protected]</a>'));
Route::get('event/new_event', array('as'=> 'new_event', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="185d6e7d766c5b77766c6a7774747d6a58767d6f5d6e7d766c">[email protected]</a>'));
Route::post('event/create', array('uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6f3c0d3d8c2f5d9d8c2c4d9dadad3c4f6d5c4d3d7c2d3">[email protected]</a>'));
Route::post('event/update', array('uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b1e2d3e352f1834352f293437373e291b2e2b3f3a2f3e">[email protected]</a>'));
Route::delete('event/delete', array('uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="713407141f05321e1f05031e1d1d14033115140205031e08">[email protected]</a>'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="622714070c16210d0c16100d0e0e071022140b0715">[email protected]</a>'));
Route::get('event/{id}/edit', array('as' => 'edit_event', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="185d6e7d766c5b77766c6a7774747d6a587d7c716c">[email protected]</a>'));

Method 2

Laravel route checks in the order they were defined.

event/new_event and event/{id} both have same route structure and so it is going to view action.

Change the order –

Route::get('event/new_event', array('as'=> 'new_event', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="632615060d17200c0d17110c0f0f0611230d06142615060d17">[email protected]</a>'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8eddecdc6dcebc7c6dcdac7c4c4cddae8dec1cddf">[email protected]</a>'));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x