mercredi 25 avril 2018

Laravel (Lumen) get id from model

In my user model i've got function that i want to return if user is following the other user. This is the function I've added:

public function ifAuthorizedFollows()
{
  return $this
    ->hasMany('App\Follow', 'user_id')
    ->when(Auth::check(), function ($query) {
      return $query->where('followed_id', $this->id);
    });
    ->when(!Auth::check(), function ($query) {
      return $query->where('followed_id', null);
    });
}

I don't know how to get id of the user that logged user is checking the profile. If I change $this->id to eg. 1 it returns the follow object - thats fine, but I need to get the id of the object that is actually seeing.

This is JSON I got when changing $this->id to 1:

{
  "id": 1,
  "name": "berde",
  (...)
  "updated_at": "2018-04-24 20:19:33",
  "pages_count": 0,
  "photos_count": 0,
  "pages": [],
  "if_authorized_follows": [
    {
        "id": 1,
        "user_id": 1,
        "followed_id": 1,
        "followed_type": 4,
        "created_at": "2018-04-25 08:32:21",
        "updated_at": "2018-04-25 08:32:21"
    }
]

}

In my function ifAuthorizedFollow I need to get ID that appear in that json and $this->id is not working. Can anyone help me?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire