Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 2 years ago.
0

My response is pretty much directly from the Laravel Eloquent Docs, and is very basic.

class Image extends Eloquent {

    public function imageable()
    {
        return $this->morphTo();
    }

}

class Staff extends Eloquent {

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

}

class Order extends Eloquent {

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

}

add the following to your images schema

$table->morphs('taggable');

now to add an image for a job, for example:

$idOfJob = 1;
$image = Image::create(['imageable_id' => $idOfJob, 'imageable_type'=>'Job', 'title'=>'title here', 'path' => 'path here']);
Last updated 9 years ago.
0

Thank you for the reply, this is very helpful.

Yes maybe I should do bit more research before posting to the forums in the future.

Sorry I am new to programming as a whole, and I am only now using the MVC pattern.

So there is nothing at all Schema wise that I would need to add to tables that have related images? Which I guess is what makes the app so scalable.

So all polymorphic relation is described in the models and it only takes the two fields id and type in the one table?

Last updated 9 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.

© 2024 Laravel.io - All rights reserved.