[solved] Referencing relationships when model is namepsaced

Hi all,

I have 2 models, \App\Request and \App\Media. \App\Request has many \App\Media references. If \App\Media wasn't namespaced, I could simply call ```php <?PHP $this->nameofAppRequestObject->getMedia('condition string'); ``` But since it is namespaced that would change to: ```php <?PHP $this->nameofAppRequestObject->getApp\Media('condition string'); ``` which is obviously messed up.

As the docs suggest is possible, I manually wrote a getMedia() function that calls getRelated('\App\Media'), but that's not quite working either.

My question is: Is manually writing a function like I have done the recommended way of doing this?


You can alias the relationship:

namespace App;

class Request
    public function initialize()
        $this->hasMany('id', 'App\Media', 'request_id', array(
            'alias' => 'media'

Then, you can freely use "media" to get the related records:

$media = $this->nameofAppRequestObject->getMedia('condition string');
$media = $this->nameofAppRequestObject->media;

Ah, I see. Thanks.