Good day. Is it possible to use an alias name when using the with() function in laravel? For an example:
$posts = Post::where(/*condition*/)->with('user as friend')->get();
Short answer is no, but you can define your relationship with the alias you want to use and eager load that.
class Post extends Model
public function user(){
return $this->belongsTo(User::class);
}
public function friend(){
return $this->user()
}
}
$posts = Post::where(/*condition*/)->with('friend')->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With