Laravel关系有很多

时间:2019-05-06 15:24:43

标签: sql laravel

我的代码:

App \ User

public function buyers()
{
    return $this->hasMany(Buyer::class);
}

public function buyerSearches()
{
    return $this->hasManyThrough(BuyerSearch::class, Buyer::class);
}

public function buyerSearchProperties()
{
    return $this->hasManyThrough(BuyerSearchProperty::class, BuyerSearch::class, 'buyer_id');
}

public function shows()
{
    return $this->hasManyThrough(Show::class, BuyerSearchProperty::class, 'buyer_search_id');
}

“测试”(当然,这不是真正的测试):

/** @test */
    public function users_give_back_the_correct_shows_count()
    {
        $user = factory(User::class)->create();

        $buyer = factory(Buyer::class)->create([
            'user_id' => $user->id,
        ]);

        [$buyerSearchA, $buyerSearchB] = factory(BuyerSearch::class, 2)->create([
            'buyer_id' => $buyer->id,
        ]);

        $buyerSearchPropertyA = factory(BuyerSearchProperty::class)->create([
            'buyer_search_id' => $buyerSearchA,
        ]);

        $buyerSearchPropertyB = factory(BuyerSearchProperty::class)->create([
            'buyer_search_id' => $buyerSearchB,
        ]);

        factory(Show::class, 2)->create([
            'buyer_search_property_id' => $buyerSearchPropertyA->id,
        ]);

        factory(Show::class, 2)->create([
            'buyer_search_property_id' => $buyerSearchPropertyB->id,
        ]);

        dd($user->shows) 

}

dd仅返回$ buyerSearchPropertyA显示的内容。如果删除$ buyerSearchPropertyA,它会正确返回$ buyerSearchPropertyB显示的内容。 我在哪里犯错?

谢谢

0 个答案:

没有答案