使用Laravel 4中的Auth :: User() - > user_group->名称打印用户组名称

时间:2014-12-11 23:01:09

标签: laravel-4 model echo

我尝试使用Auth :: user() - > user_group->名称在视图中显示用户组名称,但显然这不起作用,因为我不断尝试获取非对象的财产。

代码如下

User_Group.php模型

<?php

class User_Group extends Eloquent {

    protected $table = 'user_groups';

    public function users() {
        return $this->hasMany('User');
    }

}

User.php模型

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    public function user_group()
    {
        return $this->belongsTo('User_Group', 'user_groups_id');
    }

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

    public function isAdmin()
    {
        return $this->user_groups_id == 1;
    }

}

我的个人资料.blade.php视图

<small><p class="pull-right">{{ Auth::user()->user_group->name }}</p></small>

执行以下操作将打印用户组ID引用:

 {{ Auth::user()->user_groups_id }}

1 个答案:

答案 0 :(得分:0)

将方法重命名为group而不是user_group