我有一个属于User的Notifications模型。我希望在选择通知集合时加载用户,并且只使用names和emails加载模型。但是,当使用select方法时,查询返回null。
$notifications->load(['user' => function($query){
    $query->select(["name","email"]);
}]);如果select()方法的参数如下,则会给出所有字段:
$notifications->load(['user' => function($query){
    $query->select(["*"]    
}]);发布于 2018-06-01 02:18:48
只需要添加主键列,就可以很好地工作。
$notifications->load(['user' => function($query){
    $query->select(["ID","name","email"]);
}]);这样,任何约束都可以添加到查询中。
发布于 2018-05-31 19:37:43
也许像这样选择字段是可行的:
$notifications->load('user:id,name,email');您尝试的内容旨在添加约束,而不是选择某些字段
发布于 2020-10-11 17:40:51
如果使用select处理数据,则必须传递foreign_key。看一看
public function index()
{ 
    $employee = Employee::with(['pay_salary' => function($query){
       $query->select(['employee_id','salary_amount'])
             ->whereMonth('paying_date',\Carbon\Carbon::now()->month);
    },'getSalary:id,employeeID,salary'])->get();
    
    return View::make('admin.salary.index',['data' => $employee]);
}https://stackoverflow.com/questions/50623064
复制相似问题