Laravel Eloquent Where With

不再
<?php

namespace App\Services\v1;

use Validator;

use App\Group;
use App\GroupPrivacy;
use Illuminate\Support\Facades\File;

class GroupService {

    protected $supportedIncludes = [
        'groupCases' => 'group_cases',
        'groupUsers'  => 'group_users'
    ];

    protected $clauseProperties = [
        'visibility'
    ];

    public function getGroups($parameters) {

        if (empty($parameters)) {
            return $this->filterGroups(Group::all());
        }

        $withKeys = $this->getWithKeys($parameters);
        $whereClauses = $this->getWhereClause($parameters);

        return Group::with($withKeys)->where($whereClauses)->get();

    }

    protected function getWithKeys($parameters) {
        $withKeys = [];

        if (isset($parameters['include'])) {
            $includeParams = explode(',', $parameters['include']);
            $includes = array_intersect($this->supportedIncludes, $includeParams);
            $withKeys = array_keys($includes);
        }

        $withKeys[] = 'groupPrivacy';

        return $withKeys;
    }

    protected function getWhereClause($parameters) {
        $clause = [];

        foreach ($this->clauseProperties as $prop) {
            if (in_array($prop, array_keys($parameters))) {
                $clause[$prop] = $parameters[$prop];
            }
        }

        return $clause;
    }


}

你好,

我想从有条件的表中获取一些数据。

我的要求:/api/v1/groups?include=&visibility=0

我正在尝试获取可见性为 0 的组。但是 group_privacy 表中的可见性列。所以我在他们之间建立了关系:

群组隐私模型:

class GroupPrivacy extends Model {

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'visibility',
        'notification'
    ];

    public function groups() {
        return $this->belongsTo('App\Group', 'group_id', 'id');
    }
}

团体模式:

class Group extends Model {

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'group_category_id',
        'name',
        'description',
        'img_path'
    ];

    public function groupPrivacy() {
        return $this->hasOne('App\GroupPrivacy', 'group_id', 'id');
    }

    public function users() {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }
}

我还自动在我的$withKeys数组中添加了 groupPrivacy ......

但它给出了一个错误:

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'可见性'(SQL:select * from groupswhere ( visibility= 0))

这个错误是真的,但我怎么能说“看看 GROUP_PRIVACY 表,你是白痴!!” ?

附:Laravel 5.3

妖娆

你可以做类似的事情

$group = Group::whereHas('groupPrivacy', function($q){
    $q->where('visibility ', '=', 0);
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

laravel Eloquent where子句

来自分类Dev

Laravel 5 Eloquent AND 而不是 where

来自分类Dev

Laravel Eloquent在联接中使用WHERE代替AND

来自分类Dev

Laravel Eloquent JOIN WHERE id为null

来自分类Dev

Laravel Eloquent:where子句中的函数

来自分类Dev

Laravel Eloquent - 使用关系构建“where not”查询

来自分类Dev

Laravel 5 Eloquent where with multiple or in Clauses

来自分类Dev

Laravel 4:使用Eloquent的where中的where和whereIn

来自分类Dev

Laravel-根据动态参数扩展Eloquent where子句

来自分类Dev

Laravel / Eloquent whereIn / where带有一对集合

来自分类Dev

如何在Laravel Eloquent中使用多个where条件?

来自分类Dev

使用Eloquent Where逃离Laravel DB:raw查询

来自分类Dev

Laravel Eloquent 仅在需要时使用 where 和 order 条件

来自分类Dev

如何在 Laravel Eloquent 模型中使用 where

来自分类Dev

Laravel eloquent:接收多个ID时如何使用“where”?

来自分类Dev

Laravel/Eloquent:如何将 WHERE LIKE 用于组合字段?

来自分类Dev

如何在 Laravel 5.8 上使用 where 和 groupBy eloquent

来自分类Dev

Eloquent 的高级 where 子句

来自分类Dev

不能用where in eloquent

来自分类Dev

使用Laravel 5 / Eloquent where语句查找对象的关系对象

来自分类Dev

Eloquent / Laravel-将WHERE子句放在具有链接关系的引用表上

来自分类Dev

Laravel:调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: where()

来自分类Dev

Laravel Eloquent模型在where子句中返回带有变量的空集

来自分类Dev

Laravel Eloquent-仅在“ with”中的“ where”条件为true的情况下获取关系元素

来自分类Dev

Laravel Eloquent SQLSTATE [23000]:违反完整性约束:1052列... where子句不明确

来自分类Dev

Laravel 3 Eloquent:寻找一个“ where_nested()”的例子

来自分类Dev

Laravel:调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: where()

来自分类Dev

Laravel 4(Eloquent)无法识别where('date','<','NOW()')-完全忽略了它

来自分类Dev

使用Laravel 5 / Eloquent where语句查找对象的关系对象

Related 相关文章

  1. 1

    laravel Eloquent where子句

  2. 2

    Laravel 5 Eloquent AND 而不是 where

  3. 3

    Laravel Eloquent在联接中使用WHERE代替AND

  4. 4

    Laravel Eloquent JOIN WHERE id为null

  5. 5

    Laravel Eloquent:where子句中的函数

  6. 6

    Laravel Eloquent - 使用关系构建“where not”查询

  7. 7

    Laravel 5 Eloquent where with multiple or in Clauses

  8. 8

    Laravel 4:使用Eloquent的where中的where和whereIn

  9. 9

    Laravel-根据动态参数扩展Eloquent where子句

  10. 10

    Laravel / Eloquent whereIn / where带有一对集合

  11. 11

    如何在Laravel Eloquent中使用多个where条件?

  12. 12

    使用Eloquent Where逃离Laravel DB:raw查询

  13. 13

    Laravel Eloquent 仅在需要时使用 where 和 order 条件

  14. 14

    如何在 Laravel Eloquent 模型中使用 where

  15. 15

    Laravel eloquent:接收多个ID时如何使用“where”?

  16. 16

    Laravel/Eloquent:如何将 WHERE LIKE 用于组合字段?

  17. 17

    如何在 Laravel 5.8 上使用 where 和 groupBy eloquent

  18. 18

    Eloquent 的高级 where 子句

  19. 19

    不能用where in eloquent

  20. 20

    使用Laravel 5 / Eloquent where语句查找对象的关系对象

  21. 21

    Eloquent / Laravel-将WHERE子句放在具有链接关系的引用表上

  22. 22

    Laravel:调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: where()

  23. 23

    Laravel Eloquent模型在where子句中返回带有变量的空集

  24. 24

    Laravel Eloquent-仅在“ with”中的“ where”条件为true的情况下获取关系元素

  25. 25

    Laravel Eloquent SQLSTATE [23000]:违反完整性约束:1052列... where子句不明确

  26. 26

    Laravel 3 Eloquent:寻找一个“ where_nested()”的例子

  27. 27

    Laravel:调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: where()

  28. 28

    Laravel 4(Eloquent)无法识别where('date','<','NOW()')-完全忽略了它

  29. 29

    使用Laravel 5 / Eloquent where语句查找对象的关系对象

热门标签

归档