I have an Inbox model which looks like so:
<?php
namespace Uppdragshuset\AO\Tenant\Models;
use Illuminate\Database\Eloquent\Model;
use Uppdragshuset\AO\Tenant\Models\PM\TaskUser;
use Uppdragshuset\AO\Tenant\Models\PM\User;
class Inbox extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'inbox';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'case_id',
'index',
'workflow_id',
'task_id',
'user_id',
'group_id',
'patent_id',
'workflow_title',
'task_title',
'task_description',
'group_title',
'inactive_percentage',
'priority',
'due_at',
'created_at',
'updated_at'
];
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'USR_UID');
}
public function task_users()
{
return $this->hasMany(TaskUser::class, 'TAS_UID', 'task_id');
}
public function getTasksForUser($with = [], $user_id = null){
if ( ! $user_id) {
$user_id = request()->get('auth_user_id');
}
return $this->with($with)
->whereHas('task_users', function ($query) use ($user_id) {
$query->where('USR_UID', $user_id)
->orWhere('TU_RELATION', 2)->whereHas('group', function ($query) use ($user_id) {
$query->whereHas('users', function ($query) use ($user_id) {
$query->where('USERS.USR_UID', $user_id);
});
});
})
->where('USR_UID', '')
->orWhere('USR_UID', $user_id)->get();
}
}
The TaskUser model looks like so:
<?php
namespace Uppdragshuset\AO\Tenant\Models\PM;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;
class TaskUser extends Model
{
use Eloquence, Mappable;
protected $connection = 'processmaker';
protected $table = 'TASK_USER';
protected $maps = [
'task_id' => 'TAS_UID',
'user_id' => 'USR_UID',
'type' => 'TU_TYPE',
'relation' => 'TU_RELATION',
];
public function userOrGroup()
{
if ($this->relation == 1){
return $this->belongsTo(User::class, 'USR_UID', 'USR_UID');
}else{
return $this->belongsTo(Group::class, 'USR_UID', 'GRP_UID');
}
}
public function group()
{
return $this->belongsTo(Group::class, 'USR_UID', 'GRP_UID');
}
}
When I run the getTasksForUser
on the inbox model, it gives me this error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tenant.TASK_USER' doesn't exist (SQL: select * from
inbox
where exists (select * fromTASK_USER
whereTASK_USER
.TAS_UID
=inbox
.task_id
and (USR_UID
= 00000000000000000000000000000001 orTU_RELATION
= 2 and exists (select * fromGROUPWF
whereTASK_USER
.USR_UID
=GROUPWF
.GRP_UID
and exists (select * fromUSERS
inner joinGROUP_USER
onUSERS
.USR_UID
=GROUP_USER
.USR_UID
whereGROUP_USER
.GRP_UID
=GROUPWF
.GRP_UID
andUSERS
.USR_UID
= 00000000000000000000000000000001)))) andUSR_UID
= orUSR_UID
= 00000000000000000000000000000001)
So it is complaining that I am not connecting to the correct DB for the TASK_USER table. But the $connection
property of the TaskUser model is specified with the other db. So what am I doing wrong?