Brad
Brad

Reputation: 10660

Laravel not loading model into route

I'm currently developing a PHP Laravel app, and everything has been going well so far. However, my latest resouce called FreightRequest is resulting in extremely bizarre behavior that I cannot figure out.

My route file has the following:

Route::resource('freightrequests', 'FreightRequestController');

When I access http://localhost/freightrequests/1, laravel is not correctly binding the model to my FreightRequestController@show method.

My method looks like this:

public function show(FreightRequest $freightRequest)
{
    dd($freightRequest);

    return view('freightrequests.show', compact('freightRequest'));
}

The above results in the following dump:

FreightRequest {#429 ▼
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

In fact, if I go to a non-existant ID http://localhost/freightrequests/12343243, Laravel doesn't throw NotFoundHttpException. If I visit http://localhost/users/12343243 I get a NotFoundHttpException. All of my other resources are working fine except this one resource.

I even tried to ditch the Route::resource and replace it with:

Route::get('freightrequests/{freightrequest}', 'FreightRequestController@show')->name('freightrequests.index');

I've tried renaming the model, controller, route and everything in case something was conflicting causing Laravel to no retrieve the model. It seems to be injecting the right class instance, but not actually attempting to fetch it from the server. I've tried clearing the cache. I absolutely cannot figure out why this particular model is not working where as my other 6 or so resources are working without issue.

Any ideas?

Upvotes: 2

Views: 977

Answers (1)

Brad
Brad

Reputation: 10660

I found the problem shortly after posting the question:

public function show(FreightRequest $freightRequest)
{
   dd($freightRequest);

   return view('freightrequests.show', compact('freightRequest'));
}

Should be:

public function show(FreightRequest $freightrequest)
{
    dd($freightrequest);

    return view('freightrequests.show', compact('freightrequest'));
}

Case-sensitivity matters when trying to bind parameters, and it took me a very long time to realize that R was capital :)

Upvotes: 8

Related Questions