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 条评论
登录 后参与评论

相关文章

如何使用Laravel Eloquent创建多个Where子句查询?

Laravel 5.5 Eloquent-(可选)链接where语句

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

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

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

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

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

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

Laravel Eloquent JOIN WHERE id为null

Laravel Eloquent:where子句中的函数

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

Eloquent 的高级 where 子句

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

如何在 Laravel Eloquent 模型中使用 where

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

不能用where in eloquent

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

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

Laravel 5 Eloquent where with multiple or in Clauses

Laravel 5 Eloquent AND 而不是 where

Laravel - Eloquent where with array

如何在 Laravel Eloquent 中使用 GroupBy 和 Where 子句?

Laravel Eloquent 查询带有 where 和 wherehas 关系表

Laravel Eloquent Multiple Where with count

多个 where 条件对 Laravel eloquent 不起作用

Laravel Eloquent Where、orWhere 和 Find 在同一个 eloquent 查询中的问题

Laravel Eloquent Where json 单元格

Laravel API Eloquent Where Clause with Vue 不起作用

Laravel Eloquent - Return Where Has + Where