ElpsySec
1274
2
Eloquent

Polymorphic Relationships with Custom Column Names

Posted 3 years ago by ElpsySec

I am working with a large database that I can't edit easily so I'm trying to work with what I have.

I have a product that has a locationId and locationType. The locationId can refer to either a WebStore or LocalStore model. Currently, the locationType id is either "WEB" for WebStore or "LOCAL" for LocalStore. Is there any way to establish a polymorphic relationship with these custom column names and values?

More Detailed:

I have a Product Table:

$table->increments('id');
$table->string('name');
$table->integer('locationId')->unsigned();
$table->string('locationType');

A WebStore Table:

$table->increments('id');
$table->string('name');

A LocalStore Table:

$table->increments('id');
$table->string('name');

My Models would look something like Product Model

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

WebStore Model:

 public function Product()
 {
  return $this->morphOne('App\Models\Product', 'locationId');
  }

LocalStore Model:

 public function Product()
 {
  return $this->morphOne('App\Models\Product', 'locationId');
  }

Is there a way to specify that:

  1. locationId is the key we're looking for

  2. locationType is the type we're looking for

  3. The location type "WEB" refers to App\Models\WebStore and "LOCAL" refers to LocalStore

Thanks

If this is not possible, any advice on how to establish this relationship would help. Adding an additional column like locationIdType that holds the class name is a possibility. If this could work, could you elaborate on how to specify that locationId would be the locationId. Does what I have now work if I were to do that.

Please sign in or create an account to participate in this conversation.