Laravel 试图获得 属性 的非对象,相同 table 中的关系
Laravel Trying to get property of non-object, relationship within same table
我有一个用户模型如下:
<?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' table 中有一个 'recruiters_id' 字段,其设置为:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreign('recruiters_id')->references('id')->on('users');
});
}
所以想法是用户可以招募其他用户。但是当我尝试 {{$user->recruiter->id()}} 之类的东西时,我得到了 "Trying to get property of non-object" 错误
如果我这样做 {{print_r($user->recruiter)}} 而不是我得到下面的输出,它看起来确实像一个对象(至少在我没有经验的眼中,尽管它看起来像"object" 重复了几次,即使我只使用了 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] => y$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] => y$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
这是我第一次在同一个 table 中建立 Eloquent 关系,所以可能我在那里犯了一些错误。使用 Laravel 5.1
非常感谢任何建议,提前致谢
如果没有完全迁移,我会说问题出在这里
public function recruits()
{
return $this->hasMany('App\User','recruiters_id');
}
public function recruiter()
{
return $this->belongsTo('App\User','recruiters_id');
}
对于这两个关系,您都将 recruites_id 作为外键,即使它是 id recruits。
对于 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 在您的迁移中已正确定义)
我有一个用户模型如下:
<?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' table 中有一个 'recruiters_id' 字段,其设置为:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreign('recruiters_id')->references('id')->on('users');
});
}
所以想法是用户可以招募其他用户。但是当我尝试 {{$user->recruiter->id()}} 之类的东西时,我得到了 "Trying to get property of non-object" 错误
如果我这样做 {{print_r($user->recruiter)}} 而不是我得到下面的输出,它看起来确实像一个对象(至少在我没有经验的眼中,尽管它看起来像"object" 重复了几次,即使我只使用了 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] => y$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] => y$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
这是我第一次在同一个 table 中建立 Eloquent 关系,所以可能我在那里犯了一些错误。使用 Laravel 5.1
非常感谢任何建议,提前致谢
如果没有完全迁移,我会说问题出在这里
public function recruits()
{
return $this->hasMany('App\User','recruiters_id');
}
public function recruiter()
{
return $this->belongsTo('App\User','recruiters_id');
}
对于这两个关系,您都将 recruites_id 作为外键,即使它是 id recruits。
对于 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 在您的迁移中已正确定义)