Laravel Limit Results by Many-Many Relationship





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:



$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->paginate(10);


This worked just fine. However, I recently added search functionality that looks like this:



// search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}

$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->where('first_name', 'LIKE', '%' . $search . '%')
->orWhere('last_name', 'LIKE', '%' . $search . '%')
->orWhere('type', 'LIKE', '%' . $search . '%')
->orWhere('npi', 'LIKE', '%' . $search . '%')
->orWhere('license', 'LIKE', '%' . $search . '%')
->orWhere('dea', 'LIKE', '%' . $search . '%')
->paginate(10);


Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?










share|improve this question





























    0















    I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:



    $doctors = User::find(Auth::user()->id)
    ->doctors()
    ->orderBy($sortBy, $sort)
    ->paginate(10);


    This worked just fine. However, I recently added search functionality that looks like this:



    // search default
    if ($request->search == null) {
    $search = '';
    } else {
    $search = $request->search;
    }

    $doctors = User::find(Auth::user()->id)
    ->doctors()
    ->orderBy($sortBy, $sort)
    ->where('first_name', 'LIKE', '%' . $search . '%')
    ->orWhere('last_name', 'LIKE', '%' . $search . '%')
    ->orWhere('type', 'LIKE', '%' . $search . '%')
    ->orWhere('npi', 'LIKE', '%' . $search . '%')
    ->orWhere('license', 'LIKE', '%' . $search . '%')
    ->orWhere('dea', 'LIKE', '%' . $search . '%')
    ->paginate(10);


    Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?










    share|improve this question

























      0












      0








      0








      I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:



      $doctors = User::find(Auth::user()->id)
      ->doctors()
      ->orderBy($sortBy, $sort)
      ->paginate(10);


      This worked just fine. However, I recently added search functionality that looks like this:



      // search default
      if ($request->search == null) {
      $search = '';
      } else {
      $search = $request->search;
      }

      $doctors = User::find(Auth::user()->id)
      ->doctors()
      ->orderBy($sortBy, $sort)
      ->where('first_name', 'LIKE', '%' . $search . '%')
      ->orWhere('last_name', 'LIKE', '%' . $search . '%')
      ->orWhere('type', 'LIKE', '%' . $search . '%')
      ->orWhere('npi', 'LIKE', '%' . $search . '%')
      ->orWhere('license', 'LIKE', '%' . $search . '%')
      ->orWhere('dea', 'LIKE', '%' . $search . '%')
      ->paginate(10);


      Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?










      share|improve this question














      I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:



      $doctors = User::find(Auth::user()->id)
      ->doctors()
      ->orderBy($sortBy, $sort)
      ->paginate(10);


      This worked just fine. However, I recently added search functionality that looks like this:



      // search default
      if ($request->search == null) {
      $search = '';
      } else {
      $search = $request->search;
      }

      $doctors = User::find(Auth::user()->id)
      ->doctors()
      ->orderBy($sortBy, $sort)
      ->where('first_name', 'LIKE', '%' . $search . '%')
      ->orWhere('last_name', 'LIKE', '%' . $search . '%')
      ->orWhere('type', 'LIKE', '%' . $search . '%')
      ->orWhere('npi', 'LIKE', '%' . $search . '%')
      ->orWhere('license', 'LIKE', '%' . $search . '%')
      ->orWhere('dea', 'LIKE', '%' . $search . '%')
      ->paginate(10);


      Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?







      laravel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 15:52









      XerakonXerakon

      8710




      8710
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You need to group the orWhere clauses, otherwise your getting all doctors associated with the user and where the first_name is like $search OR where the last_name is like $search on all doctors.



          try this:



           $doctors = User::find(Auth::user()->id)
          ->doctors()
          ->orderBy($sortBy, $sort)
          ->where(function ($query) use ($search) {
          return $query->where('first_name', 'LIKE', '%' . $search . '%')
          ->orWhere('last_name', 'LIKE', '%' . $search . '%')
          ->orWhere('type', 'LIKE', '%' . $search . '%')
          ->orWhere('npi', 'LIKE', '%' . $search . '%')
          ->orWhere('license', 'LIKE', '%' . $search . '%')
          ->orWhere('dea', 'LIKE', '%' . $search . '%');
          })
          ->paginate(10);





          share|improve this answer

































            1














            You need to use when and group where



            $search = $request->input('search');

            $doctors = User::find(Auth::user()->id)
            ->doctors()
            ->when($search, function($query) use ($search){
            $query->where(function ($query) use ($search) {
            $query
            ->where('first_name', 'LIKE', '%' . $search . '%')
            ->orWhere('last_name', 'LIKE', '%' . $search . '%')
            ->orWhere('type', 'LIKE', '%' . $search . '%')
            ->orWhere('npi', 'LIKE', '%' . $search . '%')
            ->orWhere('license', 'LIKE', '%' . $search . '%')
            ->orWhere('dea', 'LIKE', '%' . $search . '%');
            })
            })
            ->orderBy('...')
            ->paginate(10);





            share|improve this answer


























            • Yes, looked at my sources - where grouping required. Corrected.

              – IndianCoding
              Nov 16 '18 at 16:21













            • @IndianCoding To make sure I am fully understanding this code, the when clause will essentially replace my if check to see if a search query has been submitted?

              – Xerakon
              Nov 16 '18 at 17:12













            • @Xerakon, You are right.

              – IndianCoding
              Nov 16 '18 at 17:35












            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%2f53341289%2flaravel-limit-results-by-many-many-relationship%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You need to group the orWhere clauses, otherwise your getting all doctors associated with the user and where the first_name is like $search OR where the last_name is like $search on all doctors.



            try this:



             $doctors = User::find(Auth::user()->id)
            ->doctors()
            ->orderBy($sortBy, $sort)
            ->where(function ($query) use ($search) {
            return $query->where('first_name', 'LIKE', '%' . $search . '%')
            ->orWhere('last_name', 'LIKE', '%' . $search . '%')
            ->orWhere('type', 'LIKE', '%' . $search . '%')
            ->orWhere('npi', 'LIKE', '%' . $search . '%')
            ->orWhere('license', 'LIKE', '%' . $search . '%')
            ->orWhere('dea', 'LIKE', '%' . $search . '%');
            })
            ->paginate(10);





            share|improve this answer






























              1














              You need to group the orWhere clauses, otherwise your getting all doctors associated with the user and where the first_name is like $search OR where the last_name is like $search on all doctors.



              try this:



               $doctors = User::find(Auth::user()->id)
              ->doctors()
              ->orderBy($sortBy, $sort)
              ->where(function ($query) use ($search) {
              return $query->where('first_name', 'LIKE', '%' . $search . '%')
              ->orWhere('last_name', 'LIKE', '%' . $search . '%')
              ->orWhere('type', 'LIKE', '%' . $search . '%')
              ->orWhere('npi', 'LIKE', '%' . $search . '%')
              ->orWhere('license', 'LIKE', '%' . $search . '%')
              ->orWhere('dea', 'LIKE', '%' . $search . '%');
              })
              ->paginate(10);





              share|improve this answer




























                1












                1








                1







                You need to group the orWhere clauses, otherwise your getting all doctors associated with the user and where the first_name is like $search OR where the last_name is like $search on all doctors.



                try this:



                 $doctors = User::find(Auth::user()->id)
                ->doctors()
                ->orderBy($sortBy, $sort)
                ->where(function ($query) use ($search) {
                return $query->where('first_name', 'LIKE', '%' . $search . '%')
                ->orWhere('last_name', 'LIKE', '%' . $search . '%')
                ->orWhere('type', 'LIKE', '%' . $search . '%')
                ->orWhere('npi', 'LIKE', '%' . $search . '%')
                ->orWhere('license', 'LIKE', '%' . $search . '%')
                ->orWhere('dea', 'LIKE', '%' . $search . '%');
                })
                ->paginate(10);





                share|improve this answer















                You need to group the orWhere clauses, otherwise your getting all doctors associated with the user and where the first_name is like $search OR where the last_name is like $search on all doctors.



                try this:



                 $doctors = User::find(Auth::user()->id)
                ->doctors()
                ->orderBy($sortBy, $sort)
                ->where(function ($query) use ($search) {
                return $query->where('first_name', 'LIKE', '%' . $search . '%')
                ->orWhere('last_name', 'LIKE', '%' . $search . '%')
                ->orWhere('type', 'LIKE', '%' . $search . '%')
                ->orWhere('npi', 'LIKE', '%' . $search . '%')
                ->orWhere('license', 'LIKE', '%' . $search . '%')
                ->orWhere('dea', 'LIKE', '%' . $search . '%');
                })
                ->paginate(10);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 16:13

























                answered Nov 16 '18 at 16:03









                RemulRemul

                1,3451414




                1,3451414

























                    1














                    You need to use when and group where



                    $search = $request->input('search');

                    $doctors = User::find(Auth::user()->id)
                    ->doctors()
                    ->when($search, function($query) use ($search){
                    $query->where(function ($query) use ($search) {
                    $query
                    ->where('first_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('last_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('type', 'LIKE', '%' . $search . '%')
                    ->orWhere('npi', 'LIKE', '%' . $search . '%')
                    ->orWhere('license', 'LIKE', '%' . $search . '%')
                    ->orWhere('dea', 'LIKE', '%' . $search . '%');
                    })
                    })
                    ->orderBy('...')
                    ->paginate(10);





                    share|improve this answer


























                    • Yes, looked at my sources - where grouping required. Corrected.

                      – IndianCoding
                      Nov 16 '18 at 16:21













                    • @IndianCoding To make sure I am fully understanding this code, the when clause will essentially replace my if check to see if a search query has been submitted?

                      – Xerakon
                      Nov 16 '18 at 17:12













                    • @Xerakon, You are right.

                      – IndianCoding
                      Nov 16 '18 at 17:35
















                    1














                    You need to use when and group where



                    $search = $request->input('search');

                    $doctors = User::find(Auth::user()->id)
                    ->doctors()
                    ->when($search, function($query) use ($search){
                    $query->where(function ($query) use ($search) {
                    $query
                    ->where('first_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('last_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('type', 'LIKE', '%' . $search . '%')
                    ->orWhere('npi', 'LIKE', '%' . $search . '%')
                    ->orWhere('license', 'LIKE', '%' . $search . '%')
                    ->orWhere('dea', 'LIKE', '%' . $search . '%');
                    })
                    })
                    ->orderBy('...')
                    ->paginate(10);





                    share|improve this answer


























                    • Yes, looked at my sources - where grouping required. Corrected.

                      – IndianCoding
                      Nov 16 '18 at 16:21













                    • @IndianCoding To make sure I am fully understanding this code, the when clause will essentially replace my if check to see if a search query has been submitted?

                      – Xerakon
                      Nov 16 '18 at 17:12













                    • @Xerakon, You are right.

                      – IndianCoding
                      Nov 16 '18 at 17:35














                    1












                    1








                    1







                    You need to use when and group where



                    $search = $request->input('search');

                    $doctors = User::find(Auth::user()->id)
                    ->doctors()
                    ->when($search, function($query) use ($search){
                    $query->where(function ($query) use ($search) {
                    $query
                    ->where('first_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('last_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('type', 'LIKE', '%' . $search . '%')
                    ->orWhere('npi', 'LIKE', '%' . $search . '%')
                    ->orWhere('license', 'LIKE', '%' . $search . '%')
                    ->orWhere('dea', 'LIKE', '%' . $search . '%');
                    })
                    })
                    ->orderBy('...')
                    ->paginate(10);





                    share|improve this answer















                    You need to use when and group where



                    $search = $request->input('search');

                    $doctors = User::find(Auth::user()->id)
                    ->doctors()
                    ->when($search, function($query) use ($search){
                    $query->where(function ($query) use ($search) {
                    $query
                    ->where('first_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('last_name', 'LIKE', '%' . $search . '%')
                    ->orWhere('type', 'LIKE', '%' . $search . '%')
                    ->orWhere('npi', 'LIKE', '%' . $search . '%')
                    ->orWhere('license', 'LIKE', '%' . $search . '%')
                    ->orWhere('dea', 'LIKE', '%' . $search . '%');
                    })
                    })
                    ->orderBy('...')
                    ->paginate(10);






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 16 '18 at 16:23

























                    answered Nov 16 '18 at 16:16









                    IndianCodingIndianCoding

                    1,0441110




                    1,0441110













                    • Yes, looked at my sources - where grouping required. Corrected.

                      – IndianCoding
                      Nov 16 '18 at 16:21













                    • @IndianCoding To make sure I am fully understanding this code, the when clause will essentially replace my if check to see if a search query has been submitted?

                      – Xerakon
                      Nov 16 '18 at 17:12













                    • @Xerakon, You are right.

                      – IndianCoding
                      Nov 16 '18 at 17:35



















                    • Yes, looked at my sources - where grouping required. Corrected.

                      – IndianCoding
                      Nov 16 '18 at 16:21













                    • @IndianCoding To make sure I am fully understanding this code, the when clause will essentially replace my if check to see if a search query has been submitted?

                      – Xerakon
                      Nov 16 '18 at 17:12













                    • @Xerakon, You are right.

                      – IndianCoding
                      Nov 16 '18 at 17:35

















                    Yes, looked at my sources - where grouping required. Corrected.

                    – IndianCoding
                    Nov 16 '18 at 16:21







                    Yes, looked at my sources - where grouping required. Corrected.

                    – IndianCoding
                    Nov 16 '18 at 16:21















                    @IndianCoding To make sure I am fully understanding this code, the when clause will essentially replace my if check to see if a search query has been submitted?

                    – Xerakon
                    Nov 16 '18 at 17:12







                    @IndianCoding To make sure I am fully understanding this code, the when clause will essentially replace my if check to see if a search query has been submitted?

                    – Xerakon
                    Nov 16 '18 at 17:12















                    @Xerakon, You are right.

                    – IndianCoding
                    Nov 16 '18 at 17:35





                    @Xerakon, You are right.

                    – IndianCoding
                    Nov 16 '18 at 17:35


















                    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%2f53341289%2flaravel-limit-results-by-many-many-relationship%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

                    Bressuire

                    Vorschmack

                    Quarantine