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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器