douguachan2879 2017-05-30 07:59
浏览 38
已采纳

Laravel Eloquent在哪里

<?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;
    }


}

Hello,

I want to get some data from table with conditions.

My request : /api/v1/groups?include=&visibility=0

I am trying to get groups which has visibility is 0. But visibility column in group_privacy table. So i made a relation between them :

GroupPrivacy Model :

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');
    }
}

Group Model :

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');
    }
}

Also i added groupPrivacy in my $withKeys array automatically...

But it gives an error :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'visibility' in 'where clause' (SQL: select * from groups where (visibility = 0))

This error true but how can i say "LOOK IN GROUP_PRIVACY TABLE, YOU HACKIN IDIOT!!" ?

ps. Laravel 5.3

  • 写回答

1条回答 默认 最新

  • dsgk40568 2017-05-30 08:11
    关注

    you can do something like

    $group = Group::whereHas('groupPrivacy', function($q){
        $q->where('visibility ', '=', 0);
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角