Davos98
Davos98

Reputation: 99

SQL Laravel Eloquent - Where field can be uppercase or lowercase

$pname can be "Airbus", "airbus" or "AIRBUS"

$info = Plane::where('plane_name', '=', $pname)
                    ->where('uid', '=', $uid)
                    ->get();

Is there any way I can update this query to check the database for the plane name without case sensitivity?

Upvotes: 1

Views: 9551

Answers (4)

Muhammad Umar
Muhammad Umar

Reputation: 105

To solve my issue, I have to do that on my Model. simple copy and paste this function

public function __get($key)
    {

        if (is_null($this->getAttribute($key))) {
            return $this->getAttribute(strtoupper($key));
        } else {
            return $this->getAttribute($key);
        }

    }

Upvotes: 0

Fame.K Ch
Fame.K Ch

Reputation: 1

Add -> use Illuminate\Support\Facades\DB; in Controller file.

-----------Example---------------
UserInfo::where(DB::raw('upper(username)'), strtoupper($Request->username))

It's work for me, hope to help you.

Upvotes: 0

hery
hery

Reputation: 32

just make sure result where clause to lowercase and query clause to lowercase too.

where(DB::raw('lower(column_name)'), '=', Str::lower($query))

dont forget to use:

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

Upvotes: 0

Vinie
Vinie

Reputation: 2993

Use LOWER MYsql function and strtolower php function

$info = Plane::whereRaw('LOWER(plane_name) = (?)', [strtolower($pname)])
                    ->where('uid', '=', $uid)
                    ->get();

OR

Plane::where('plane_name', 'ilike', $pname)->where('uid', '=', $uid)
                    ->get();

Upvotes: 2

Related Questions