0

In my Model class; Currently I have defined like;

return $this->belongsTo(related: Site::class, foreignKey:'SiteId', ownerKey:'SiteId');; ----->This works.

But I want to define a combination as Foreign key, eg: CompanyCode+SiteId

My Current and Target model has both columns(ie:CompanyCode+SiteId). That combination will return a single entry.I want to retrieve that in my current model.

How can I do that?

My Site Model is like;

class Site extends Model
{
    protected $table = 'vwSitesPortal';
    protected $primaryKey = 'SiteId';

...

My Current model is like;

class Alarm extends Model
{
 protected $table = 'vwAlarm';
    protected $primaryKey = 'AlarmId';
...
  public function Site()
    {
       return $this->belongsTo(**related**: Site::class,
**foreignKey**:'SiteId', **ownerKey:**'SiteId'
    }
Ratha
  • 9,434
  • 17
  • 85
  • 163

1 Answers1

-1
Schema::create('favorites', function (Blueprint $table) {

$table->integer('lecture_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();

$table->primary(['lecture_id', 'user_id']);

$table->foreign('lecture_id')
      ->references('id')->on('lectures')
      ->onDelete('cascade');

$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');

});

see this example
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26