Laravel Eloquent Owned Doesn't Work

I cannot get the relationship belongsToto work (or I'm using the wrong relationship).

My database structure (simplified):

pages

id | title | main_image
-----------------------
1 | Test | 5

mass media

id | filepath
-----------------------
5 | uploads / test.jpg

So, I want to be able to do $page->main_image, and it will return me an instance of the Media model, so I could use $page->main_image->filepath, etc.

In the model Page, I have the following:

    public function main_image()
    {
        return $this->belongsTo('App\Modules\Media\Models\Media', 'id', 'main_image');
    }

But when I do $page->main_image, I just get int 5. Am I using the wrong relationship here?

Thank!

+4
source share
1 answer

$page->main_image Eloquent main_image(), . main_image. ( ), . main_image_id.

/ :

  • (public $main_page)
  • GetAccessor ($page->getMainPage())
  • / ($page->attributes['main_page'])
  • / ($this->attributes['main_page'])
  • ($this->relations['main_page'])
  • ($this->main_page()->get())
+2

Source: https://habr.com/ru/post/1653129/


All Articles