0

I'm working on a new project using Laravel where I need to use data from a legacy database(so please ignore the database design).

This database is using the username as primary key:

enter image description here

The User model has a One To Many relationshio with "ChangeContract" model

/**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function changesContract(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(ChangeContract::class, 'Username', 'UserName')->latestFirst();
    }

The current user has multiple contract changes:

enter image description here

But only two Contract Changes are returned by the relationship, the ones that matach the primary key(user name) exactly(case sensitive):

   "changesContract": [
        {
            "type": "Contract Change",
            "username": "FroneaBA",
            "date": "2017-10-01",
            "endDate": "2018-06-30"
        },
        {
            "type": "Contract Change",
            "username": "FroneaBA",
            "date": "2017-07-01",
            "endDate": "2017-09-30"
        }
    ],

I need also to return the items where the key is froneaba. Is there a way to set the primary key to be case insensitive?

P.S: From the database client, if I write the query by hand, all the items are returned:

SELECT * FROM changescontract WHERE username='froneaba';
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
NicuVlad
  • 2,491
  • 3
  • 28
  • 47
  • use the `LIKE` keyword to check the string without considering its Upper or Lower case. – odaiwa Oct 09 '22 at 14:31
  • @odaiwa can you post an example on how this would work in a relationship? – NicuVlad Oct 10 '22 at 06:08
  • In my opinion this is not a duplicated questions, I fixed it using this library: https://github.com/TishoTM/eloquent-ci-relations, please vote reopen – NicuVlad Nov 10 '22 at 10:16
  • 1
    @odaiwa In most database system, `LIKE` is not case insensitive (unless a case-insensitive collation is used) – Mark Rotteveel Nov 14 '22 at 09:59

0 Answers0