0
votes

i am working on search module in laravel.. when i submit a keyword in search input at the result i get only one record.. but i have multiple user on a single name. how to get multiple users on same name.. i tried this

Controller

class SearchController extends Controller
{
    public function find(Request $request)
    {
    $search=$request->input('search-input');


    $getUser = User::join('personalinfos', 'users.id', '=', 'personalinfos.user_id')->where('users.firstName', 'like', '%' . $search . '%')
        ->orWhere('users.lastName', 'like', '%' . $search . '%')->get();


    return view('pages/search-users',compact('getUser'));


    }
}

Form

<form class="navbar-form navbar-right hidden-sm" id="search-form" action="{{url('/find')}}" method="get">
      {{  csrf_field() }}
      <div class="form-group">
        <i class="icon ion-android-search"></i>
        <input type="text" class="form-control" id="search-input" name="search-input"  placeholder="Search friends, photos, videos">
      </div>
    </form>

Route

Route::get('find', 'SearchController@find');
1
Does every user have personalinfos?Autista_z
I am not sure what are you doing? FIrst of all you are running two queries and you are replacing result of first query with second one then whats the point of having first query at all :))Abhay Maurya
@Autista_z yes every user haveJamal Ahmad
What is the DDL of your personalinfos ?Pankit Gami
@Learner sorry by mistake.. review it again i update itJamal Ahmad

1 Answers

1
votes

I guess you are trying to do this:

$getUser = User::join('personalinfos AS pi', 'users.id', '=', 'pi.user_id')
               ->where('users.firstName', 'like', '%' . $search . '%')
               ->orWhere('users.lastName', 'like', '%' . $search . '%')
               ->select('users.firstName','users.lastName')
               ->get();

If it still returns only one user, that means your condition:

users.id = pi.user_id

is only true for that user.

To make sure, run this:

$getUser = User::where('firstName', 'like', '%' . $search . '%')
                   ->orWhere('lastName', 'like', '%' . $search . '%')
                   ->select('firstName','lastName')
                   ->get();

This last one must return more than one user if you have more than one user of same name in your database.

Good luck!