0

I have a relatively simple variable set that seems to work out fine, but I believe it is how I am outputting it that creates an issue.

At the moment I use the following in my controller:

    if(empty($_GET['direction'])){
        $direction = "'LIKE', '%'";
        } else if ($_GET['direction'] === 'Inbound') {
        $direction = "'>', 1";
        } else if ($_GET['direction'] === 'Outbound'){
        $direction = "'=', 1";
    }

Now, I have dd'd the $direction after the fact and every test shows that it accurately chose the correct variable option given the value of $_GET['direction'].

My problem lies with how I am either outputting it there or inputting the results in the below section:

$manifests = DB::table('carrier_manifests')
        ->join('customers', 'carrier_manifests.carrierOrigin', '=', 'customers.id')
        ->select('carrier_manifests.*', 'customers.customer_name')  

        ->where([
                    ['manifestNumber', 'LIKE', '%' . $manifest . '%'],
                    ['originTerminal','LIKE','%' . $terminal . '%'],
                    ['carrierOrigin', $direction],
                ])
        ->orderBy('dateUnloaded', 'DESC')
        ->whereBetween('dateUnloaded', [$startDate, $endDate])
        ->limit(100)
        ->get();

Now, before I go on, I will say that everything works correctly here and has for some time, it's when the carrierOrigin and $direction are added that there is an issue.

For example if I leave the direction field empty on the page, it will set the $direction variable as equal to "'LIKE', '%'".

Unfortunately, in the where clause, this isn't sent well or something, as it returns no results, but as a test if I change the this line:

['carrierOrigin', $direction],

to

['carrierOrigin','LIKE','%'],

it returns all of the results successfully. So is there a different way I should format the output or send the output?

1
  • What you should do is pass the operator and value as separate parameters to the where in the builder, what you're doing now is saying that "LIKE %" is the value. Commented May 25, 2018 at 3:57

1 Answer 1

1

You are passing string as second and third where arguments. Use this in controller:

if(empty($_GET['direction'])){
    $direction = ['LIKE', '%'];
    } else if ($_GET['direction'] === 'Inbound') {
    $direction = ['>', 1];
    } else if ($_GET['direction'] === 'Outbound'){
    $direction = ['=', 1];
}

Then on query:

$manifests = DB::table('carrier_manifests')
    ->join('customers', 'carrier_manifests.carrierOrigin', '=', 'customers.id')
    ->select('carrier_manifests.*', 'customers.customer_name')  

    ->where([
                ['manifestNumber', 'LIKE', '%' . $manifest . '%'],
                ['originTerminal','LIKE','%' . $terminal . '%'],
                ['carrierOrigin', $direction[0], $direction[1],
            ])
    ->orderBy('dateUnloaded', 'DESC')
    ->whereBetween('dateUnloaded', [$startDate, $endDate])
    ->limit(100)
    ->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.