Support the ongoing development of Laravel.io →
Eloquent

I have two tables:

images
-id
-name
-imageable_id
-imageable_type

article
-id
-title
....
-image_id

I've created a images function to get all images that returns this relationship:

public function images()
{
    return $this->morphMany('App\Image', 'imageable');
}

and that works great, but I also want to have a mainImage function that will use image_id on article to get the image with that id from the image table. I've tried

hasOne('App\Image', 'id', 'image_id')

With this code

$imageId = $request->input('mainImage');
if(!empty($imageId)){
    $image = Image::find($imageId);
    $listing->mainImage()->save($image);
}

And that doesn't seem to set image_id to anything but null. Thank you in advance for any help!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.