dimanche 3 avril 2016

Laravel current user in model

I need to get Auth::id() in my model to check if current user has voted. How to access to current user in Eloquent Model?

Model: namespace App;

use App\User;
use App\ArticleVote;
use Illuminate\Database\Eloquent\Model;

class Article extends Model {
  protected $fillable = ['title', 'body', 'link'];
  protected $appends = ['votesCount', 'isUserVoted'];

  public function getIsUserVotedAttribute() {
    return !!$this->votes()->where('user_id', \Auth::id())->first();
  }
}

In getIsUserVotedAttribute method I get \Auth::id() of null



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire