Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel dependency injection not working

I'm using resource controllers in my laravel app and I'm trying to use dependancy injection but it's not working on a particular model and controller.

This works:

/**
* Display the specified resource.
*
* @param  \App\Booking  $booking
* @return \Illuminate\Http\Response
*/
public function show(Booking $booking)
{
    return $booking;
}

But for some infuriating reason this doesn't:

/**
 * Display the specified resource.
 *
 * @param  \App\SchoolEvent  $schoolEvent
 * @return \Illuminate\Http\Response
 */
public function show(SchoolEvent $schoolEvent)
{
    return $schoolEvent;
}

My Routes look like this:

// Events
Route::resource('events', Resources\SchoolEventController::class);

// Bookings
Route::resource('bookings', Resources\BookingController::class);

For some reason /bookings/1 returns a filled object but /events/1 returns an empty one. Can anyone tell me why?

like image 374
IainChambers Avatar asked Mar 20 '17 16:03

IainChambers


1 Answers

Change the injected variable name to $event to match the route parameter name {event}:

public function show(SchoolEvent $event)

You can see the route parameters with this command:

php artisan route:list
like image 68
dparoli Avatar answered Oct 26 '22 04:10

dparoli