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