【问题标题】:October CMS Query Builder can't get relation model十月 CMS 查询生成器无法获取关系模型
【发布时间】:2017-07-28 14:31:16
【问题描述】:

谁能解释这种行为?

例如,两个模型:

人,国家

人民属于国家:

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: [],
   }                                                                                                                                                                                                               
>>>

为什么?

【问题讨论】:

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


    【解决方案1】:

    您应该首先获取 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() 返回查询构建器对象

    【讨论】:

    • 我在寻找方法,而不是动态属性。我的错误是我尝试将方法应用于许多条目。我必须获取一个条目,然后调用方法,如下所示:Test\TestPlugin\Models\People::find(1)->country()->get();
    【解决方案2】:

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

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

    然后查询构建器返回该条目的关系,而不是全部;

    【讨论】:

      【解决方案3】:

      您可以将protected $with = ['country']; 添加到您的People's 模型中以预先加载与$person = People::with('country')->find(1); 相同的关系

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-02
        • 2019-03-30
        • 2018-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多