phplaraveldependency-injectionlaravel-6route-model-binding

How much model name matter in injecting into a function in controller[Base on a problem]?


days before i was working on my homework which a problem cause me many days to fix and it was weird for me but somehow I've got that problem again which i'm pretty sure it's about how i name my model when I'm injecting it into function. Here's the full issues

Before when i was going to delete/update data with api i was using :

public function destroy(Event $event, EventGallery $eventGallery)
{

    $eventGallery->delete();
    return response(null,Response::HTTP_NO_CONTENT);
}

And i saw nothing going to happen, then I've changed $eventGallery to $gallery then my problem solved and with this, I've seen this problem was in 2 more functions.

In my Event Model i had relation with gallery name and also it's in protected field, now there's a problem like this again, but it's not working at all, i also had EventVideo $videos and it wasn't working, then i changed it to $video and it worked.

Right now base on above information i need to know why this happen? How changing $eventGallery or $videos to $gallery or $video[related with videos name in Event model and protected with videos fix this?


Solution

  • It is route model binding concept, if your url is

    has parameter

    {eventGallery}
    

    you have to use

    EventGallery $eventGallery
    

    if you want {gallery}

    then you need to use

    EventGallery $gallery