Laravel 如何加载相关 table 的特定列?

Laravel how to load specific column of related table?

我有下面的代码,我想要的是取相关表的特定列。

auth()->user()->load(['business', 'passwordSecurity']);

对于 select 关系中的特定列,您应该可以这样做:

auth()->user()->load([
    'business' => function ( $query ) {
        $query->select('id', 'title');
    },
    'passwordSecurity',
]);

或者像这样:

auth()->user()->load(['business:id,name', 'passwordSecurity']);

请注意,您必须 select 形成此关系所需的 ID 和外键约束。