douguachan2879 2017-05-29 23: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 00:11
    关注

    you can do something like

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部