SPresnac's avatar

SPresnac liked a comment+100 XP

2mos ago

Despite the hype around AI coding tools, which are indeed very helpful, we still need a solid understanding of fundamentals such as relationships, queues and jobs, DDD, TDD, REST APIs, models, broadcasting, and more. We are in charge of AI, not the other way around. That’s why we still need to learn and truly understand programming concepts, languages, and paradigms, and in this particular case, the Laravel ecosystem. For this reason, platforms like Laracasts remain extremely valuable.

SPresnac's avatar

SPresnac wrote a reply+100 XP

5mos ago

Meh, thanks for the hint. So, one needs to build a way around this ...

SPresnac's avatar

SPresnac started a new conversation+100 XP

5mos ago

I do have a relation in one of my models, that uses a different db-connection.

So, some code: Model Employee:

class Employee extends Model
{
    protected $connection = 'kpidashboard';
    protected $table = 'employees';

    protected $fillable = [
        'employee_name',
        'kuerzel',
        'username',
        'active_employee',
    ];

    public function user(): HasOne
    {
        return $this->hasOne(
            related: User::class,
            foreignKey: 'username',
            localKey: 'kuerzel',
        );
    }

}

I do use this in another model, that uses the default mariadb connection:

class SageAufFSchrift extends Model
{
 // lots of other code
    public function responsible(): HasOne
    {
        return $this->hasOne(
            related: Employee::class,
            foreignKey: 'kuerzel',
            localKey: 'free_c03',
        );
    }

What I want to do now is: SageAufFSchrift::query()->whereRelation('responsible', 'username', 'someusername');

But it ends with

Base table or view not found:   1146 Table 'crm.employees' doesn't exist  ....

Where crm is the mariadb-schema for my connection.

Is Eloquent just missing to use the connection or am I doing something wrong here?

SPresnac's avatar

SPresnac started a new conversation+100 XP

5mos ago

Hej,

I do have a hasOne relation like this

    public function employee(): HasOne
    {
        return $this->hasOne(
            related: Employee::class,
            foreignKey: 'username',
            localKey: 'username',
        );
    }

Problem is: The "Employee" class is extern (like in an external DB, I do have only read access). Some Usernames are written in lowercase, some are mixed and I do not know when applies what.

So, back in the days, I would make a lowercase compare on both sides and the problem would have fixed with that. But how do I do this with eloquents relation classes?

Simply setting the "foreignKey" to "lower('username')" surely is not working. How to fix this?

SPresnac's avatar

SPresnac wrote a reply+100 XP

5mos ago

Thank you @rihulfaakbar , this is the solution to my problem. Thanks a lot for it. wire:key is making it happen.

SPresnac's avatar

SPresnac wrote a reply+100 XP

5mos ago

I did update my origin post and added some code, hope it helps...