mardi 2 août 2016

belongsTo and/or hasMany usage in Laravel 5

I am used to CakePHP and Phalcon where models are linked together using a foreign key and a belongsTo and/or hasMany etc by setting it once. So every time when I get the output of a query I can easily access linked table values.

In CakePHP I do this from the User model like:

public $hasMany = array('UserType');

In Phalcon I do this from the User model like:

public function initialize() {
    $this->hasMany('id', 'Models\UserType', 'usertype_id', array('alias' => 'UserType'));
}

Where in both cases when I get the results for a user I can easily access for example the UserType name using $user->UserType->name.

But as I am reading for Laravel this is done through a function like:

$user->getUserType->name;

I think this is extra work and it is not accessible in a blade, I am hoping that there is a simple solution just like in CakePHP and Phalcon



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire