vendredi 26 mai 2017

do a check if id does not exist do something

I am trying to find if user has sent a friend request or not. if user has not sent any request I would like to show send request button.

So I have a table called friends

id
user_id
friend_id
reqs_status which can be 0,1,2

reqs_status = 0 meaning request already sent and waiting for approval
reqs_status = 1 meaning show Accept or Reject button
reqs_status = 2 meaning they are friends already
No row existing meaning not sent any request [show send request button]

If user A sends request to B. I create 2 rows one saying
user_id 1 sent a request to friend_id 2 with reqs_status as 0 and 
another as inverse user_id 2 got request from friend_id 1 with reqs_status as 1

Here reqs_status play key role in showing right buttons on screen. like: already taken user, friend request sent, send request, accept/reject button,etc..

Now my data looks something like:

[  
   {  
      "id":9,
      "firstname":"Hattie",
      "lastname":"Cropper",
      "email":"hgc3@org.uk",
      "role":1,
      "friends":[  
         {  
            "id":7,
            "user_id":8,
            "friend_id":9,
            "reqs_status":2,

         }
      ]
   },
   {  
      "id":21,
      "firstname":"Jon",
      "lastname":"Olson",
      "email":"jo185@org.uk",
      "role":1,
      "friends":[  
         {  
            "id":19,
            "user_id":20,
            "friend_id":21,
            "reqs_status":2,

         },
         {  
            "id":29,
            "user_id":23,
            "friend_id":21,
            "reqs_status":1,

         }
      ]
   },
   {  
      "id":24,
      "firstname":"Jessica",
      "lastname":"Jensen",
      "email":"jj61@org.uk",
      "role":1,
      "friends":[  
         {  
            "id":24,
            "user_id":25,
            "friend_id":24,
            "reqs_status":2,

         },
         {  
            "id":30,
            "user_id":23,
            "friend_id":24,
            "reqs_status":0,
         }
      ]
   }
]

if no request sent meaning there is no data in friends table.

@foreach ($user['friends'] as $friend)
@if($friend['reqs_status'] == 0 && $friend['user_id'] == $myid)
//show button already sent
@elseif($friend['reqs_status'] == 1 && $friend['user_id'] == $myid)
//show accept reject button
@elseif ($friend['user_id']== '')
//show send request button
@endif
@endforeach

I cant do @else because there are array of friends and for each of those other friends there would be a button.Instead I want to use elseif check if user_id does not exist in a friends array list then show send request button.

Above condition : @elseif ($friend['user_id']== '') fails. I dont know how to compare something that does not exists in the first place :/



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

Aucun commentaire:

Enregistrer un commentaire