Search code examples
phplaravellaravel-5laravel-5.3

Passing array of values in join query and get corresponding result in laravel


Im using laravel in my project. In that the array of values from another i got but i dont know how to get array result from given array i dont know how to use where condition.

Below is my query

$requests = ProfileRequest::where('user_id', $user_id)->pluck('requested_id')->toArray();
    $values = is_array($requests)? array_values($requests): array();
    $users = DB::table('users')
            ->leftJoin('basic_informations', 'users.id', '=', 'basic_informations.user_id')
            ->leftJoin('physical_attributes', 'users.id', '=', 'physical_attributes.user_id')
            ->leftJoin('religion_and_ethnicity', 'users.id', '=', 'religion_and_ethnicity.user_id')
            ->leftJoin('occupational_attributes', 'users.id', '=', 'occupational_attributes.user_id')
            ->leftJoin('personal_interests', 'users.id', '=', 'personal_interests.user_id')
            ->select('users.id', 'users.user_name', 'basic_informations.age', 'basic_informations.unique_id', 'basic_informations.mother_tongue', 'religion_and_ethnicity.religion_id', 'religion_and_ethnicity.caste_id', 'occupational_attributes.education_id', 'occupational_attributes.occupation_id')
            ->whereNotNull('basic_informations.user_id')
            ->whereNotNull('physical_attributes.user_id')
            ->whereNotNull('religion_and_ethnicity.user_id')
            ->whereNotNull('occupational_attributes.user_id')
            ->whereIn('users.id', '=', $values)->get();
        print_r($users);

when i try the above it shows error

ErrorException in Grammar.php line 254: Array to string conversion


Solution

  • Probably the error is this:

    ->whereIn('users.id', '=', $values)
    

    Change this line to:

    ->whereIn('users.id', $values)