title: Laravel Model Relationship - Search not working
body: <p>Laravel Model Relationship - Search not working
  it wil fetched more records which is not needed</p>
  <p>1)Relation: define function in cmsUser.php (Model)</p>
  <pre><code>    public function userDetails()
      {
          return $this-&gt;hasOne('App\Models\UserDetail', 'user_id', 'id');
      }
  </code></pre>
  <p>2)code implemnt in userrepo.php</p>
  <pre><code>    $cmsUser = CmsUser::with('userDetails');
  
      if ($request-&gt;has('search') &amp;&amp; $request['search']!=&quot;&quot;) {
          $searchString = trim($request['search']);
          $cmsUser-&gt;whereHas('userDetails', function ($query) use ($searchString) {
                $query-&gt;whereNotNull('emp_code')
                      -&gt;where('first_name', 'ilike', '%' . $searchString . '%')
                      -&gt;orWhere('last_name', 'ilike', &quot;%&quot; . $searchString . &quot;%&quot;)
                      -&gt;orWhere('email', 'ilike', &quot;%&quot; . $searchString . &quot;%&quot;)
                      -&gt;orWhere('contact_number', $searchString);
          });           
      }
  
      if($admin_type != 'superadmin'){
          if (isset($organisation_id) &amp;&amp; $organisation_id != &quot;&quot;) {
              $cmsUser-&gt;where('organisation_id', $organisation_id);
          }
      }
      
      $cmsUser-&gt;whereNull('admin_type')-&gt;orWhereIn('admin_type', ['hq','store']);
      $userList = $cmsUser-&gt;orderByDesc('updated_at')-&gt;paginate($perPage, '*', 'page', $page)-&gt;toArray();
  </code></pre>