Laravel Eloquent 使用带有 with() 函数的别名
日安。在 laravel 中使用 with() 函数时是否可以使用别名?例如:
$posts = Post::where(/*condition*/)->with('user as friend')->get();
日安。在 laravel 中使用 with() 函数时是否可以使用别名?例如:
$posts = Post::where(/*condition*/)->with('user as friend')->get();
简短的回答是否定的,但您可以定义您与要使用的别名的关系,并预先加载该别名。
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();