Hanny
Hanny

Reputation: 2159

Laravel 5 Routing Calling Wrong View

I've got a simple Laravel 5 site that I am working on (learning Laravel)

I have a link on my 'users' view to add a 'new user':

<a href="{{ url('/user/create') }}">Create New User</a>

My user Routes look like the following:

Route::get('/users', 'UserController@index');
Route::get('/user/{id}', 'UserController@edit');
Route::get('/user/create', 'UserController@create');
Route::get('/user/update', 'UserController@update');
Route::get('/user/delete/{id}', 'UserController@delete');

My UsersController has the following:

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    $users = user::all();
    return view('user.index',compact('users'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    $userroles = userrole::all();
    return view('user.create', compact('userroles'));
}

When I click the link - I get the following error:

Trying to get property of non-object (View: C:\xampp\htdocs\mysite\resources\views\user\edit.blade.php)

I cannot figure out why it is trying to load edit.blade.php

I have tried just putting the following in my create view and it still doesn't render.

@extends('app')

@section('content')
Test
@endsection

I'm not sure why the routing is being so bizarre.

Any ideas?

Upvotes: 4

Views: 1695

Answers (1)

Pawel Bieszczad
Pawel Bieszczad

Reputation: 13325

Put the create (and update) route before the edit route in your routes file. Laravel 1st stumbles on the edit route and treats the create as id.

You can test it by doing a dd($id) in your edit method in the controller.

Upvotes: 3

Related Questions