Implicit Route Model Binding returns empty model in package












0















I'm building a laravel package the encapsulates a rest api, and I'm running into some issues with implicit route model binding. All I'm getting back when trying to retrieve a single record is an empty array. The id that i'm trying to retrieve is present in the database (its the only record in the table) Using debugbar, it looks like its not running the query, which implies that the route binding is failing before it has a chance to run (more on that at the bottom).



api.php:



Route::apiResources([
'trackers' => TrackerController::class,
'tracker/entry' => TrackerEntryController::class,
'tracker/types' => TrackerTypeController::class
]);


pertinent artisan route:list output:



| Method    | URI                   | Middleware |
+-----------+-----------------------+------------+
| GET|HEAD | tracker/entry/{entry} | |
| GET|HEAD | tracker/types/{type} | |
| GET|HEAD | trackers/{tracker} | |
+-----------+-----------------------+------------+


Show method from TrackerTypeController:



use OxthornTrackersModelsTrackerType as Type;

public function show(Type $type)
{
return $type;
}


So, as far as I see, my code is using the correct naming scheme for implicit route binding.



If I change the controller show method to this:



public function show(Type $type, $id)
{
$type2 = Type::findOrFail($id);
return [
[get_class($type), $type->exists, $type],
[get_class($type2), $type2->exists],
];

}


I get this output:



[
[
"Oxthorn\Trackers\Models\TrackerType",
false,

],
[
"Oxthorn\Trackers\Models\TrackerType",
true
]
]


This seems to mimic the behavior in this StackOverflow issue: Implicit Route Model Binding, where the last posted theory was that SubstituteBindings middleware was not running. I'm not sure at this point what steps I need to take to ensure its running before my code execute, so I am here asking for advice on where to go from here.










share|improve this question



























    0















    I'm building a laravel package the encapsulates a rest api, and I'm running into some issues with implicit route model binding. All I'm getting back when trying to retrieve a single record is an empty array. The id that i'm trying to retrieve is present in the database (its the only record in the table) Using debugbar, it looks like its not running the query, which implies that the route binding is failing before it has a chance to run (more on that at the bottom).



    api.php:



    Route::apiResources([
    'trackers' => TrackerController::class,
    'tracker/entry' => TrackerEntryController::class,
    'tracker/types' => TrackerTypeController::class
    ]);


    pertinent artisan route:list output:



    | Method    | URI                   | Middleware |
    +-----------+-----------------------+------------+
    | GET|HEAD | tracker/entry/{entry} | |
    | GET|HEAD | tracker/types/{type} | |
    | GET|HEAD | trackers/{tracker} | |
    +-----------+-----------------------+------------+


    Show method from TrackerTypeController:



    use OxthornTrackersModelsTrackerType as Type;

    public function show(Type $type)
    {
    return $type;
    }


    So, as far as I see, my code is using the correct naming scheme for implicit route binding.



    If I change the controller show method to this:



    public function show(Type $type, $id)
    {
    $type2 = Type::findOrFail($id);
    return [
    [get_class($type), $type->exists, $type],
    [get_class($type2), $type2->exists],
    ];

    }


    I get this output:



    [
    [
    "Oxthorn\Trackers\Models\TrackerType",
    false,

    ],
    [
    "Oxthorn\Trackers\Models\TrackerType",
    true
    ]
    ]


    This seems to mimic the behavior in this StackOverflow issue: Implicit Route Model Binding, where the last posted theory was that SubstituteBindings middleware was not running. I'm not sure at this point what steps I need to take to ensure its running before my code execute, so I am here asking for advice on where to go from here.










    share|improve this question

























      0












      0








      0








      I'm building a laravel package the encapsulates a rest api, and I'm running into some issues with implicit route model binding. All I'm getting back when trying to retrieve a single record is an empty array. The id that i'm trying to retrieve is present in the database (its the only record in the table) Using debugbar, it looks like its not running the query, which implies that the route binding is failing before it has a chance to run (more on that at the bottom).



      api.php:



      Route::apiResources([
      'trackers' => TrackerController::class,
      'tracker/entry' => TrackerEntryController::class,
      'tracker/types' => TrackerTypeController::class
      ]);


      pertinent artisan route:list output:



      | Method    | URI                   | Middleware |
      +-----------+-----------------------+------------+
      | GET|HEAD | tracker/entry/{entry} | |
      | GET|HEAD | tracker/types/{type} | |
      | GET|HEAD | trackers/{tracker} | |
      +-----------+-----------------------+------------+


      Show method from TrackerTypeController:



      use OxthornTrackersModelsTrackerType as Type;

      public function show(Type $type)
      {
      return $type;
      }


      So, as far as I see, my code is using the correct naming scheme for implicit route binding.



      If I change the controller show method to this:



      public function show(Type $type, $id)
      {
      $type2 = Type::findOrFail($id);
      return [
      [get_class($type), $type->exists, $type],
      [get_class($type2), $type2->exists],
      ];

      }


      I get this output:



      [
      [
      "Oxthorn\Trackers\Models\TrackerType",
      false,

      ],
      [
      "Oxthorn\Trackers\Models\TrackerType",
      true
      ]
      ]


      This seems to mimic the behavior in this StackOverflow issue: Implicit Route Model Binding, where the last posted theory was that SubstituteBindings middleware was not running. I'm not sure at this point what steps I need to take to ensure its running before my code execute, so I am here asking for advice on where to go from here.










      share|improve this question














      I'm building a laravel package the encapsulates a rest api, and I'm running into some issues with implicit route model binding. All I'm getting back when trying to retrieve a single record is an empty array. The id that i'm trying to retrieve is present in the database (its the only record in the table) Using debugbar, it looks like its not running the query, which implies that the route binding is failing before it has a chance to run (more on that at the bottom).



      api.php:



      Route::apiResources([
      'trackers' => TrackerController::class,
      'tracker/entry' => TrackerEntryController::class,
      'tracker/types' => TrackerTypeController::class
      ]);


      pertinent artisan route:list output:



      | Method    | URI                   | Middleware |
      +-----------+-----------------------+------------+
      | GET|HEAD | tracker/entry/{entry} | |
      | GET|HEAD | tracker/types/{type} | |
      | GET|HEAD | trackers/{tracker} | |
      +-----------+-----------------------+------------+


      Show method from TrackerTypeController:



      use OxthornTrackersModelsTrackerType as Type;

      public function show(Type $type)
      {
      return $type;
      }


      So, as far as I see, my code is using the correct naming scheme for implicit route binding.



      If I change the controller show method to this:



      public function show(Type $type, $id)
      {
      $type2 = Type::findOrFail($id);
      return [
      [get_class($type), $type->exists, $type],
      [get_class($type2), $type2->exists],
      ];

      }


      I get this output:



      [
      [
      "Oxthorn\Trackers\Models\TrackerType",
      false,

      ],
      [
      "Oxthorn\Trackers\Models\TrackerType",
      true
      ]
      ]


      This seems to mimic the behavior in this StackOverflow issue: Implicit Route Model Binding, where the last posted theory was that SubstituteBindings middleware was not running. I'm not sure at this point what steps I need to take to ensure its running before my code execute, so I am here asking for advice on where to go from here.







      laravel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 4:34









      ZamerickZamerick

      115




      115
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You know, sleeping on a problem does wonders. For anyone that runs into this same issue while developing a package, I had to change my route code to this to solve the issue:



          Route::apiResource('trackers', TrackerController::class)->middleware('bindings');
          Route::apiResource('tracker/entry', TrackerEntryController::class)->middleware('bindings');
          Route::apiResource('tracker/types', TrackerTypeController::class)->middleware('bindings');





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975310%2fimplicit-route-model-binding-returns-empty-model-in-package%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You know, sleeping on a problem does wonders. For anyone that runs into this same issue while developing a package, I had to change my route code to this to solve the issue:



            Route::apiResource('trackers', TrackerController::class)->middleware('bindings');
            Route::apiResource('tracker/entry', TrackerEntryController::class)->middleware('bindings');
            Route::apiResource('tracker/types', TrackerTypeController::class)->middleware('bindings');





            share|improve this answer




























              0














              You know, sleeping on a problem does wonders. For anyone that runs into this same issue while developing a package, I had to change my route code to this to solve the issue:



              Route::apiResource('trackers', TrackerController::class)->middleware('bindings');
              Route::apiResource('tracker/entry', TrackerEntryController::class)->middleware('bindings');
              Route::apiResource('tracker/types', TrackerTypeController::class)->middleware('bindings');





              share|improve this answer


























                0












                0








                0







                You know, sleeping on a problem does wonders. For anyone that runs into this same issue while developing a package, I had to change my route code to this to solve the issue:



                Route::apiResource('trackers', TrackerController::class)->middleware('bindings');
                Route::apiResource('tracker/entry', TrackerEntryController::class)->middleware('bindings');
                Route::apiResource('tracker/types', TrackerTypeController::class)->middleware('bindings');





                share|improve this answer













                You know, sleeping on a problem does wonders. For anyone that runs into this same issue while developing a package, I had to change my route code to this to solve the issue:



                Route::apiResource('trackers', TrackerController::class)->middleware('bindings');
                Route::apiResource('tracker/entry', TrackerEntryController::class)->middleware('bindings');
                Route::apiResource('tracker/types', TrackerTypeController::class)->middleware('bindings');






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 30 '18 at 17:41









                ZamerickZamerick

                115




                115






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975310%2fimplicit-route-model-binding-returns-empty-model-in-package%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas