使用关系名称“user”时,BelongsTo关系返回null

时间:2017-11-10 11:16:42

标签: laravel laravel-5 eloquent phpunit laravel-5.5

在Laravel 5.5中创建简单的一对一关系时,每当我使用方法/关系名称require 'rails_helper' RSpec.describe OfficeHourScheduler do describe '#active_office_holiday' do it 'returns the active holidays of the company' do company = FactoryBot.create(:company) active_holiday = FactoryBot.create(:office_holiday, company: company, active: true) inactive_holiday = FactoryBot.create(:office_holiday, company: company, active: false) expect(described_class.new(company).active_office_holiday).to match_array [active_holiday] end end end 时,$person->user都会返回null值。如果我将名称更改为userfooUser,则代码似乎正常。这是我有同样问题的第二个项目。谁能看到我做错了什么?

In Person模型:

login

在PersonTest中:

public function user() {
    return $this->belongsTo(User::class, 'user_id', 'id');
}
public function foo() {
    return $this->belongsTo(User::class, 'user_id', 'id');
}
public function getUser() {
    if ($this->user_id) {
        return User::find($this->user_id);
    } else {
        return null;
    }
}

根据要求,这里是与Person有关的所有代码, 整个App \ Models \ Person.php:

$user = factory(User::class)->create();
$person = factory(Person::class)->create(['user_id' => $user->id]);

// This works                 
$this->assertTrue( $person->getUser()->is($user) );

// This works
$this->assertTrue( !is_null($person->foo) );
if ( $person->foo ) {
    $this->assertTrue( $person->foo->is($user) );
}

// This fails
$this->assertTrue( !is_null($person->user) );
if ( $person->user ) {
    $this->assertTrue( $person->user->is($user) );
}

性状:

use App\Models\User;
use App\Models\Asset;
use App\Traits\HasGuid;
use App\Traits\HasNotes;
use App\Traits\HasModifiedBy;
use App\Traits\HasAttachments;
use App\Traits\HasRelationships;
use App\Transformers\PersonTransformer;
use App\Models\Abstracts\HasTypeModelAbstract;
use App\Models\Interfaces\HasTypeModelInterface;

class Person extends HasTypeModelAbstract implements HasTypeModelInterface {

    use HasModifiedBy,
        HasNotes,
        HasAttachments,
        HasRelationships;

    protected $fillable = [
        'person_type_id',
        'email',
        'fname',
        'lname',
        'user_id',
        'modified_by_user_id',
        'audited_at',
        'custom_attributes'
    ];
    protected $casts = [
        'custom_attributes' => 'json',
        'user_id' => 'integer',
        'modified_by_user_id' => 'integer',
        'person_type_id' => 'integer'
    ];
    protected $dates = [
        'audited_at'
    ];

    public static $transformer = PersonTransformer::class;

    public function user() {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function type() {
        return $this->belongsTo(PersonType::class, 'person_type_id');
    }

    public function assets() {
        return $this->hasMany(Asset::class, 'person_id');
    }

/应用/型号/文摘/ HasTypeModelAbstract

trait HasNotes {

    protected static function bootHasNotes() {
        static::deleting(function ($instance) {
            $instance->notes->each(function ($note) {
                $note->delete();
            });
        });
    }

    public function notes() {
        return $this->morphMany(Note::class, 'notable');
    }

}

trait HasModifiedBy {

    protected static function bootHasModifiedBy() {
        static::saving(function ($instance) {
            $instance->modified_by_user_id = Auth::id();
        });
    }

    public function modifiedBy() {
        return $this->belongsTo(User::class, 'modified_by_user_id');
    }

}

trait HasAttachments {

    protected static function bootHasAttachments() {

        static::deleting(function ($instance) {
            $instance->attachments->each(function ($attachment) {
                $attachment->delete();
            });
        });
    }

    public function attachments() {
        return $this->morphMany(Attachment::class, 'attachable');
    }

}

trait HasRelationships {

    protected static function bootHasRelationships()
    {
        static::deleting(function ($instance) {
            Relation::forObject( $instance )->delete();
        });
    }


    public function related() { ...[long polymorphic relationship here]... }

2 个答案:

答案 0 :(得分:1)

我必须诚实 - 快速查看代码我不会发现任何错误,但这并不代表一切都是肯定的。

如果我是你,我会尝试将Person模型限制为:

class Person extends \Illuminate\Database\Eloquent\Model {

    protected $fillable = [
        'person_type_id',
        'email',
        'fname',
        'lname',
        'user_id',
        'modified_by_user_id',
        'audited_at',
        'custom_attributes'
    ];
    protected $casts = [
        'custom_attributes' => 'json',
        'user_id' => 'integer',
        'modified_by_user_id' => 'integer',
        'person_type_id' => 'integer'
    ];
    protected $dates = [
        'audited_at'
    ];

    public static $transformer = PersonTransformer::class;

    public function user() {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function type() {
        return $this->belongsTo(PersonType::class, 'person_type_id');
    }

    public function assets() {
        return $this->hasMany(Asset::class, 'person_id');
    }

}

现在我会验证一切是否正常。如果没问题,现在你可以进一步调查,添加一个特征并验证,添加第二个特征并验证,最后从同一个类扩展。

某处肯定存在错误,但看着这段代码很难找到任何东西

答案 1 :(得分:0)

用户是eloquent中的保留名称。

尝试用户而不是用户

public function User() {
return $this->belongsTo(User::class, 'user_id', 'id');
}