首页 > 解决方案 > 我怎样才能只在 Laravel 的“hasManyThrough”关系中提取一列?

问题描述

Building::with('user_through_building')
                      ->where('id', $building_id)
                      ->pluck('user_through_building.id');

我不断收到错误Unknown column 'user_through_building.id' in 'field list' (SQL: select 'user_through_building'.'id' from 'buildings' where 'id' = 20 and 'buildings'.'deleted_at' is null)

标签: phplaravel

解决方案


// Retrieve all buildings that have at least one user_through_building
return Building::has('user_through_building')->get(['id']);

// Returns all Buildings, along with user_through_building' IDs
return Building::with('user_through_building:id')->get();

推荐阅读