Murlidhar Fichadia Murlidhar Fichadia - 1 year ago 57
PHP Question

Finding a user profile match in laravel 5.3

I have a user who speaks native language lets say English and wants to learn French.

How can I find a list of match where users native is French and wants to learn English.
Its a language exchange program so I would like to list users with the match.

I have Users, Language_User and Language table. in Language_User table I have an extra field called type(learn or native).

Users



id

name

...

Language_User



id

user_id

language_id

type('learn','native')

Language



id

language

Queries



First I am getting all the users that does not have a role as 'Admin' or is not him/herself.

$other_users = User::with('languages')->with('departments')->with('hobbies')->with('universities')->with('years')->where([['id', '<>', Auth::user()->id],['role', '<>', 2]])->get();


Then, I am getting loggedin users $learn language and $native language to compare with other users

$learn = Auth::user()->languages()->wherePivot('type', 'learn')->select('language_id')->first();

$native = Auth::user()->languages()->wherePivot('type', 'native')->select('language_id')->first();


In View



gives loggedin user language to learn

{{$learn->language_id}}


gives loggedin user native language

{{$native->language_id}}


View Code

@foreach($other_users as $user)

@foreach($user->languages as $lang)

@if($lang['pivot']['type'] ==='native' AND $lang['pivot']['language_id'] ===$learn['language_id'])

<tr>
<td>{{$user->firstname}} </td>
<td>{{$user->lastname}} </td>
<td>{{$user->email}} </td>
<td>{{$user->photo}} </td>
<td>{{$user->bio}} </td>
<td>{{$user['universities']->university}} </td>
<td>{{$user['departments']->department}} </td>
<td>{{$user['years']->year}} </td>

<td>
@foreach($user->languages as $lang)
@if($lang['pivot']['type'] === 'native')
{{$lang["language"]}}
@endif
@endforeach
</td>

<td>
@foreach($user->languages as $lang)
@if($lang['pivot']['type'] === 'learn')
{{$lang["language"]}}
@endif
@endforeach
</td>

<td>
@foreach($user->hobbies as $hobby)
{{$hobby->hobby}} <br>
@endforeach
</td>
</tr>
@endif
@endforeach
@endforeach


I am unable to put two if conditions one for checking native language and one for checking learn language. if I do like this:

@if($lang['pivot']['type'] ==='native' AND $lang['pivot']['language_id'] ===$learn['language_id'])
@if($lang['pivot']['type'] ==='learn' AND $lang['pivot']['language_id'] ===$native['language_id'])


I get no rows, whereas user do exist with a match.

I think because I have two pivot type learn and native in an array. how to compare both learn and native? below shown languages format. its many to many relation

"languages":[
{
"id":3,
"language":"Spanish",
"pivot":{
"user_id":2,
"language_id":3,
"type":"native"
}
},
{
"id":4,
"language":"Greek",
"pivot":{
"user_id":2,
"language_id":4,
"type":"learn"
}
}
]

Answer Source

Instead of loop through all the users in the database you could just constrain your query:

$native = Auth::user()->languages()->wherePivot('type', 'native')->first();

$learn = Auth::user()->languages()->wherePivot('type', 'learn')->first();

$other_users = User::with('departments', 'hobbies', 'universities', 'years', 'languages')
    ->where('id', '<>', Auth::user()->id)
    ->where('role', '<>', 2)
    ->whereHas('languages', function ($query) use ($learn) {
        $query->where('type', 'native')->where('id', $learn->id);
    })
    ->whereHas('languages', function ($query) use ($native) {
        $query->where('type', 'learn')->where('id', $native->id);
    })
    ->get();

Also, just an FYI, $lang['pivot']['type'] can also be written as:

$lang->pivot->type;

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download