【问题标题】:Laravel Trying to get property of non-object, relationship within same tableLaravel试图获取非对象的属性,同一张表中的关系
【发布时间】:2016-07-07 15:19:24
【问题描述】:

我有一个用户模型如下:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    public function recruits()
    {
        return $this->hasMany('App\User','recruiters_id');
    }
    public function recruiter()
    {
        return $this->belongsTo('App\User','recruiters_id');
    }
}

“users”表中有一个“recruiters_id”字段,设置如下:

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->foreign('recruiters_id')->references('id')->on('users');
        });
    }

所以这个想法是用户可以招募其他用户。但是当我尝试类似 {{$user->recruiter->id()}} 时,我得到“尝试获取非对象的属性”错误

如果我这样做 {{print_r($user->recruiter)}} 而不是我得到下面的输出,它看起来确实像一个对象(至少在我没有经验的眼睛看来,虽然它看起来像“对象”重复几次,即使我只使用一次 print_r):

App\User Object ( [table:protected] => users [fillable:protected] => Array ( [0] => review_count [1] => review_sum [2] => review_avg [3] => display_name [4] => business_name [5] => setting1 [6] => referrer_id [7] => referrer_margin [8] => gender [9] => age [10] => title [11] => points [12] => credits [13] => bankacct_nr [14] => bankacct_name [15] => bsb [16] => save_payment [17] => newsletter [18] => first_name [19] => last_name [20] => phone [21] => email [22] => password [23] => subject [24] => credentials [25] => margin [26] => user_type [27] => url [28] => questions [29] => form_subject [30] => postcode [31] => recruiters_id [32] => internal_notes ) [hidden:protected] => Array ( [0] => password [1] => remember_token ) [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 240 [first_name] => Jane [last_name] => Recruit [title] => [phone] => [email] => i@recruit.com [user_type] => recruiter [margin] => 0.00 [credentials] => [password] => $2y$10$eTSX9vRGFr5eupAL/kyexeARVDjT.M/7xE2Yp.6PMTuH/dOsFd6QS [remember_token] => [created_at] => 2016-07-06 15:19:23 [updated_at] => 2016-07-06 15:19:23 [status] => [url] => [newsletter] => 0 [postcode] => [form_subjects] => [questions] => [save_payment] => 0 [bsb] => [bankacct_nr] => [bankacct_name] => [points] => 0 [credits] => 0 [gender] => [age] => 0 [referrer_id] => [referrer_margin] => 0.00 [setting1] => [business_name] => [display_name] => i recruit [following] => [followers] => [belts_id] => [recruiters_id] => [internal_notes] => [review_count] => 0 [review_sum] => 0 [review_avg] => 0.00 ) [original:protected] => Array ( [id] => 240 [first_name] => Jane [last_name] => Recruit [title] => [phone] => [email] => i@recruit.com [user_type] => recruiter [margin] => 0.00 [credentials] => [password] => $2y$10$eTSX9vRGFr5eupAL/kyexeARVDjT.M/7xE2Yp.6PMTuH/dOsFd6QS [remember_token] => [created_at] => 2016-07-06 15:19:23 [updated_at] => 2016-07-06 15:19:23 [status] => [url] => [newsletter] => 0 [postcode] => [form_subjects] => [questions] => [save_payment] => 0 [bsb] => [bankacct_nr] => [bankacct_name] => [points] => 0 [credits] => 0 [gender] => [age] => 0 [referrer_id] => [referrer_margin] => 0.00 [setting1] => [business_name] => [display_name] => i recruit [following] => [followers] => [belts_id] => [recruiters_id] => [internal_notes] => [review_count] => 0 [review_sum] => 0 [review_avg] => 0.00 ) [relations:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [dateFormat:protected] => [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] => ) 1 

这是我第一次在同一个表中设置 Eloquent 关系,所以我可能在那里犯了一些错误。使用 Laravel 5.1

任何建议将不胜感激,在此先感谢

【问题讨论】:

  • 能否提供你的用户表迁移代码(完整)

标签: php laravel eloquent laravel-5.1


【解决方案1】:

如果没有完全迁移,我会说问题出在这里

public function recruits()
{
    return $this->hasMany('App\User','recruiters_id');
}
public function recruiter()
{
    return $this->belongsTo('App\User','recruiters_id');
}

对于这两种关系,您将recruites_id 作为外键,即使它的id 是新兵。

hasMany 你应该有

return $this->hasMany('App\User', 'foreign_key', 'local_key');

belongsTo 也一样

return $this->belongsTo('App\User', 'foreign_key', 'other_key');

因此

public function recruits()
{
    return $this->hasMany('App\User','recruiters_id', 'id');
}
public function recruiter()
{
    return $this->belongsTo('App\User');
}

应该是您正在寻找的(如果在您的迁移中正确定义了recruiters_id)

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 2017-09-02
    • 2017-02-22
    • 2020-09-28
    • 2018-02-10
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多