0

How to give alias in replated table hasMany relationship.

Controller

$templates = UserMaster::where ( 'INACTIVE', '=', 0 )->with('contact')->get ();

Model

public function contact(){
    return $this->hasMany(\App\Models\Contact::class,'CONTACT_ID');
}

I can try this code my but give error

$templates = UserMaster::where ( 'INACTIVE', '=', 0 )->with('contact as CONTACT')->get ();

So how give alias in with query?

Thank you

Jimit H.
  • 185
  • 1
  • 11
  • The 'with' function is not translated 1:1 to SQL. Thus an alias on relations is not possible without further ado. If you want to do this, define a method to rename the retrieved data. – Philipp Nies Sep 12 '19 at 05:39
  • So you mean directly not possible? – Jimit H. Sep 12 '19 at 05:41
  • I can't imagine. Then you would have to work directly with the DB::class – Philipp Nies Sep 12 '19 at 05:42
  • 1
    Maybe you look here, there was an additional function to solve your problem. https://stackoverflow.com/questions/25549526/how-to-alias-in-laravel-eager-loading – Philipp Nies Sep 12 '19 at 05:44

0 Answers0