php - 需要帮助构建查询 laravel 5.2

标签 php mysql sql laravel-5

我正在尝试从数据库中获取戒指。但只有过滤器是主页是 1 或 0。

我只需要主页为 1 的行。

enter image description here

这是我试过的

        $ringen = RingKoppelCategory::with('ringen')->get()->where('homepage', '=' , 1);

返回空值

当我将 ->get() 放在查询构建器的末尾时,它会检查 ringkoppelcategory 表中的主页,这不是我想要的。

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'homepage' in 'where clause' (SQL: select * from `ringkoppelcategory` where `homepage` = 1

我需要从 ringkoppel 类别中获取环关系,但只有主页为 1 的环。

最佳答案

您需要使用函数在您的with 中传递where

$ringen = RingKoppelCategory::with(['ringen' => function ($query) {
    $query->where('homepage', '=' , 1);
}])->get();

更多信息可在 documentation 中找到

关于php - 需要帮助构建查询 laravel 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38224402/

相关文章:

php - 如何在 Apache (Windows) 中保护我的 PHP 应用程序文件夹?

PHP 按行比较两个文本文件

php - Doctrine2/Symfony2 : One-Way Many-to-Many Query with Left Join

mysql - 使用express js、node js和Angular 6以及mysql数据库从数据库进行电子邮件验证

php - 查询以获取关于其发票编号具有相同日期的所有行?

mysql - SQL查询错误: Unclosed Quote

php - 通过 unix 脚本运行 php

php - 我们如何使用 API 访问特定的 Google Analytics(分析)帐户数据?

php - 有没有办法将事件处理函数附加到 mysql 错误?

sql - 按某些列 SQL 进行透视(转置)