0

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 * from TASK_USER where TASK_USER.TAS_UID = inbox.task_id and (USR_UID = 00000000000000000000000000000001 or TU_RELATION = 2 and exists (select * from GROUPWF where TASK_USER.USR_UID = GROUPWF.GRP_UID and exists (select * from USERS inner join GROUP_USER on USERS.USR_UID = GROUP_USER.USR_UID where GROUP_USER.GRP_UID = GROUPWF.GRP_UID and USERS.USR_UID = 00000000000000000000000000000001)))) and USR_UID = or USR_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?

Rohan
  • 13,308
  • 21
  • 81
  • 154
  • Do you have configured db for that connection in config/database.php ? You have to add to config-file something like this: http://i.imgur.com/XUXIZKG.jpg – Alex Slipknot Apr 20 '17 at 07:51
  • Yes of course I have. And it is working too :) – Rohan Apr 20 '17 at 07:55
  • Ok then you can clear config cache. Here link you might looking for: https://laravel.io/forum/05-27-2014-use-eloquent-relationships-with-another-database Also this can help you: http://stackoverflow.com/questions/32422593/laravel-belongsto-relationship-with-different-databases-not-working – Alex Slipknot Apr 20 '17 at 07:56
  • @Rohan could you resolve your problem sir? – DolDurma Jun 30 '18 at 16:33
  • @DolDurma I am not sure but I think I went to those models and explicitly specified the table names. I think that worked. Try setting `protected $table = 'table_name'` in both the models you are working with – Rohan Jul 03 '18 at 21:08

0 Answers0