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']);
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?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community