eloquent - October CMS Query Builder can't get relation model -


can explain behavior?

in example, 2 models:

people, country

people belongsto country:

public $belongsto = [                                                                                                                                                                                               'country' => [                                                                                                                                                                                                          'test\testplugin\models\country',                                                                                                                                                                               ]    

i created entries of each model, , associate it.

there tinker dump:

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

i see people#1 has relation country#1, when try relation in query builder, return empty collection:

>>> test\testplugin\models\people::country()->get();                                                                                                                                                                => october\rain\database\collection {#970      all: [],    }                                                                                                                                                                                                                >>> 

why?

you should first people model , can country specific model.

something this:

$people = test\testplugin\models\people::all(); foreach ($people $person){     $country = $person->country;     # country } 

ps. $person->country returns country model , $person->country() returns query builder object


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -