无法在Laravel中获取关系属性

时间:2015-12-11 13:23:21

标签: laravel-5 eloquent relation

我在Laravel 5中遇到关系问题。问题是我有一个表用户,并且该用户属于 Group ,为此我有这个用户模型:

public function group(){
    return $this->belongsTo('App\models\Group');
}

模型具有以下属性:name,unity,level,init_date。我还在那里放了一个默认函数来将一个组作为String返回,这就是代码:

public function __toString(){
        return $this->name.' Unity '.$this->unity;
    }

所以,在一个视图中有很多用户和每个用户的东西我想显示统一性,名称,日期。当我调用$user->group时,它正确地返回一个字符串中的名称和统一(因为_toString函数),这意味着他确实是在完美地查询该组,但是当我想要访问一个简单的属性为unity时使用$user->group->name Laravel的日期或名称给出了这个错误:

Trying to get property of non-object

我甚至尝试了$user->group()->name然后我得到了:未定义的属性:Illuminate\Database\Eloquent\Relations\BelongsTo::$name

编辑:

模型用户:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    protected $table = 'users';

    protected $fillable = ['email', 'password','estate','filial_id','perfil_id','rol','cat_teacher'];

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

    public function __toString(){
        return $this->email;
    }

    public function filial(){
            return $this->belongsTo('App\models\Filial');
    }

    public function perfil(){
        return $this->belongsTo('App\models\Perfil','perfil_id');
    }

    public function grupo(){
        return $this->belongsTo('App\models\Group','group_id','id');
    }
}

模特组:

class Group extends Model {


protected $table = 'groups';

protected $fillable = ['name','unity','date'];

public function filiales() {
   return $this->belongsTo('App\models\Filial');
}

public function teacher(){
    return $this->belongsTo('App\models\User','teacher_id');
}

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

然后,在控制器中我做了dd($users)并且没有出现关系,出现其他关系但不是这个。在视图中我想打印一个表中的一些属性,为此我有:

<td>{{$user->group}}</td>
<td>{{$user->group->unity}}</td>

第一行完美无缺,我不知道为什么。

1 个答案:

答案 0 :(得分:0)

您无法返回public class Test { private class MyRunnable implements Runnable { @Override public void run() { System.err.println("Thread name: " + Thread.currentThread().getName()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public Test() { for (int i = 1; i <= 2; i++) { final int f = i; ThreadFactory factory = new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, "Thread Name " + f); } }; Thread thread = factory.newThread(new MyRunnable()); thread.start(); } } public static void main(String[] args) { new Test(); while (true) { // Wait all threads print their names } } } 名称的原因是group返回查询构建器的实例,而不是一个雄辩的集合/对象。由于->group()属于单个user,因此请使用以下两种方式之一修改代码:

group

然后使用以下方法之一访问该组:

public function group(){
    return $this->belongsTo('App\models\Group')->first();
}

或者,保持$user = User::with("group")->where("id", "=", 1)->first(); $group = $user->group; echo $group->name; // OR $user = User::where("id", "=", 1)->first(); $group = $user->group(); echo $group->name; 功能不变,然后访问group()对象上的->group()->first()

$user

上述任何一种方法都应正确返回您的$user = User::where("id", "=", 1)->first(); $group = $user->group()->first(); echo $group->name; 对象的名称(或其他属性)。有关如何访问这些对象的详细说明,请查看Eloquent Documentation