10月CMS Query Builder无法获得关系模型

时间:2017-07-28 14:31:17

标签: eloquent query-builder relation octobercms laravel-query-builder

任何人都可以解释这种行为吗?

例如,两个模型:

人,国家

人属于国家:

public $belongsTo = [                                                                                                                                                                                          
    'country' => [                                                                                                                                                                                                 
        'Test\TestPlugin\Models\Country',                                                                                                                                                                          
    ]   

我创建了每个模型的条目,并将其关联起来。

有修补程序转储:

>>> Test\TestPlugin\Models\People::all();                                                                                                                                                                          
=> October\Rain\Database\Collection {#926                                                                                                                                                                          
     all: [                                                                                                                                                                                                        
       Test\Testplugin\Models\People {#928                                                                                                                                                                         
         id: 1,                                                                                                                                                                                                    
         country_id: 1,                                                                                                                                                                                            
       },                                                                                                                                                                                                          
       Test\Testplugin\Models\People {#930                                                                                                                                                                         
         id: 2,                                                                                                                                                                                                    
         country_id: 0,                                                                                                                                                                                            
       },                                                                                                                                                                                                          
     ],                                                                                                                                                                                                            
   }     

>>> Test\TestPlugin\Models\People::with('country')->get();                                                                                                                                                         
=> October\Rain\Database\Collection {#963
     all: [
       Test\Testplugin\Models\People {#943
         id: 1,
         country_id: 1,
         country: Test\Testplugin\Models\Country {#965
           id: 1,
           name: "Russia",
         },                                                                                                                                                                                                        
       },                                                                                                                                                                                                          
       Test\Testplugin\Models\People {#945
         id: 2,
         country_id: 0,
         country: null,
       },                                                                                                                                                                                                          
     ],                                                                                                                                                                                                            
   }         

我看到People#1与Country#1有关系,但当我尝试在查询构建器中获取此关系时,它返回空集合:

>>> Test\TestPlugin\Models\People::country()->get();                                                                                                                                                               
=> October\Rain\Database\Collection {#970
     all: [],
   }                                                                                                                                                                                                               
>>>

为什么?

3 个答案:

答案 0 :(得分:0)

您应首先获取People模型,然后您可以获取特定模型的Country。

这样的事情:

$people = Test\TestPlugin\Models\People::all();
foreach ($people as $person){
    $country = $person->country;
    # do something with the country
}

PS。 $person->country返回国家/地区模型,$person->country()返回查询构建器对象

答案 1 :(得分:0)

相反,要在整个模型上调用关系方法,首先需要选择一个条目:

Test\TestPlugin\Models\People::find(1)->country()->get();

然后查询构建器返回关系完全针对此条目,而不是全部;

答案 2 :(得分:0)

您可以将protected $with = ['country'];添加到您的人员模型中,以便加载与$person = People::with('country')->find(1);

相同的关系

然后,您可以将该关系作为属性$person->country->name访问,同时尝试People::with('country')->get()而不是People::country()->get()

相关问题