如何查询不完整的输入

I want to transform query string like pending, shipped or cancel to number status.

$q = strtolower($keyword);

if($q == 'pen' or $q == 'pend' or $q == 'pending' ) {
    $d = 1;
} elseif($q == 'shi' or $q == 'ship' or $q == 'shipped') {
    $d = 2;
} elseif($q == 'can' or $q == 'cancel' ) {
    $d = 3;
} else {
    $d = 4;
}

$query->whereStatus($d);

Current query working fine but too much or. It's possible to do in shorten way?

str_is(query, stringToSearch) will probably be enough:

if (str_is('pen*', $q)) {
    $d = 1;
}

Else you could parse them from arrays:

$pendingArray = ['pen', 'pend', 'pending'];
if (in_array($q, $pendingArray)) {
    $d = 1;
}

If these are all the conditions you need, you could always use a switch.

$q = strtolower($keyword);

$d = 4;

switch($q) {
    case 'pen':
    case 'pend':
    case 'pending':
    case 'pen':
        $d = 1;
        break;
    case 'shi':
    case 'ship':
    case 'shipped':
        $d = 2;
        break;
    case 'can':
    case 'cancel':
        $d = 3;
        break;
}

$query->whereStatus($d);

If this needs to be called on a model, it could be saved to a Laravel scope function like so:

on Laravel model

public function scopeSearchStatus($query, $keyword) {
    /** All the code above **/
}

Then it can be called cleanly anywhere you'd like:

SomeModel::searchStatus($keyword);

You could also try this:

<?php
$q = strtolower($keyword);

$d = (preg_match('/\b(pen|pend|pending)\b/', $q)) ? 1 : 4;
$d = (preg_match('/\b(shi|ship|shipped)\b/', $q)) ? 2 : 4;
$d = (preg_match('/\b(can|cancel|)\b/', $q)) ? 3 : 4;

$query->whereStatus($d);