Skip to content

Instantly share code, notes, and snippets.

@iRynoh
Created February 4, 2020 17:02
Show Gist options
  • Save iRynoh/a3cdd54a91fe1b2bfb5de3f9da6e72a6 to your computer and use it in GitHub Desktop.
Save iRynoh/a3cdd54a91fe1b2bfb5de3f9da6e72a6 to your computer and use it in GitHub Desktop.
Trying to get dynamic relationship in Laravel 6
<?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