mardi 7 février 2017

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

  

gives loggedin user native language




@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> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td> </td>

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

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

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

I am unable to put to 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"
      }
   }
]



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kD2W5I
via IFTTT

Aucun commentaire:

Enregistrer un commentaire