Created
February 4, 2020 17:02
-
-
Save iRynoh/a3cdd54a91fe1b2bfb5de3f9da6e72a6 to your computer and use it in GitHub Desktop.
Trying to get dynamic relationship in Laravel 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// InventoryEmail Model | |
public function firstReply() | |
{ | |
return $this->belongsTo(InventoryEmail::class, 'first_reply_id', 'id'); | |
} | |
public function scopeWithFirstReply(EloquentBuilder $query) | |
{ | |
$query->addSelect([ | |
'first_reply_id' => function (Builder $query) { | |
$query->select('id') | |
->from('emails as inbound') | |
->whereColumn('inbound.id_account', 'emails.id_account') | |
->whereColumn('inbound.id_listing', 'emails.id_listing') | |
->whereColumn('inbound.id_type_direction', INBOUND) | |
->whereColumn('inbound.id_type_communication', COMM_INBOUND) | |
->limit(1); | |
}, | |
])->with('firstReply')->get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment