如何在Laravel中实现Gravatar? [英] How do I implement Gravatar in Laravel?

查看:111
本文介绍了如何在Laravel中实现Gravatar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中实现Gravatar URL的最快方法是什么?我有一个必填的电子邮件地址字段,但是我不想为Gravatars创建新列,并且我希望使用本机Auth::user()属性.

What's the fastest way to implement Gravatar URLs in Laravel? I have a mandatory email address field, but I don't want to create a new column for Gravatars, and I'd prefer to use the native Auth::user() attributes.

推荐答案

结果是,您可以使用Laravel变异器来创建模型中不存在的属性.假设您有一个User模型,在相应的users表中带有一个必填的email列,只需将其粘贴在您的User模型中:

Turns out, you can use a Laravel mutator to create attributes that don't exist in your model. Assuming you have a User model with a mandatory email column in the corresponding users table, just stick this in your User model:

public function getGravatarAttribute()
{
    $hash = md5(strtolower(trim($this->attributes['email'])));
    return "http://www.gravatar.com/avatar/$hash";
}

现在,当您执行此操作时:

Now when you do this:

Auth::user()->gravatar

您将获得所需的gravatar.com URL.无需创建gravatar列,变量,方法或其他任何东西.

You'll get the gravatar.com URL you're expecting. Without creating a gravatar column, variable, method, or anything else.

这篇关于如何在Laravel中实现Gravatar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆