php - 在 Laravel Eloquent 中选择特定列以及所有相关模型

标签 php mysql laravel laravel-5 laravel-5.4

我想从主模型以及所有相关子模型中检索特定列,如下所示,

public function entries() {       
    return $this->hasMany('App\AwardEntry', 'award_id')
                ->join('users', 'users.id', 'user_id')
                ->where('users.department_id', Auth::user()->department_id)
                ->with(['files', 'reviews', 'user']);
}

这与 Award::with('entries')->findOrFail($id) 配合得很好。

现在我想从条目表及其所有子关系模式中检索entry_status

所以我添加了 select 语句,如下所示,

->select('award_entries.status')

现在工作正常,但 entries 返回空列表。

最佳答案

试试这个:

Award::select('award_entries.status')->where('id', $id)->with('entries')->get();

关于php - 在 Laravel Eloquent 中选择特定列以及所有相关模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45077519/

相关文章:

mysql - 通过has_many关系进行SQL/RoR ActiveRecord聚合查询

mysql - 我需要帮助来加快这个 select 语句的速度

MySQL错误: 1395 Can not delete from join view

php - Laravel 4 从 URL 中删除 public

phpmyadmin - php 7.4 中缺少 mbstring 扩展

php - 将 Zoho CRM 链接到 Kashflow 时在何处存储 XML 或 PHP 文件

php - 在网站上接受付款

php - Laravel 添加请求到 url

sql-server - 如何将 Laravel 7 连接到 Microsoft SQL Server?

php - 如何在 Laravel 迁移中使用自定义文件名?