<?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 /组包括= &知名度= 0
我想获得具有可视性组为0,但在group_privacy表知名度列。所以我做了他们之间的关系:
GroupPrivacy型号:
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');
}
}
此外,我加入groupPrivacy我$withKeys
自动阵列...但它给出错误:
SQLSTATE [42S22]:列未找到:1054未知列 '知名度' 在 'where子句'(SQL:SELECT * FROM
groups
其中(visibility
= 0))
此错误事实,但怎么说呢“看看GROUP_PRIVACY TABLE,你哈哈IDIOT !!” ?
ps。 Laravel 5.3
这似乎是两个表之间的关系是不存在的。哪条线是错误的原因? – Jiro90
'whereHas'来查询关系。 – Chay22
如果我写user_id而不是可见性,它的工作原理。由于group_privacies中的组表中的user_id。 ()子句有效,但where()子句不起作用,如果with()的表中的where参数... – Nevermore