Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Found the answer thank you!

$query = DB::table('Reports');

if ($published == true)
    $query->where('published', '=', 1);

if (isset($year))
    $query->where('year', '>', $year);

$result = $query->get();
Last updated 9 years ago.
0

Of course it's possible! You are very close to the answer actually :D Instead of this:

if ((int)$textToSearch){
    ->Where('Reports.ReportId', '=', $textToSearch)
    ->orWhere('Reports.ItemId', '=', $textToSearch);
}

Do this:

if ((int)$textToSearch){
    $reports
        ->Where('Reports.ReportId', '=', $textToSearch)
        ->orWhere('Reports.ItemId', '=', $textToSearch);
}

By the way, casting string to int to check if it's there is bad idea. Use this instead:

if ($testToSearch !== '')
0

Thank you, the reason to cast it's to detect an integer and query only the id fields. Thank you for your response.

0

What happened if there are 2 consecutive "where"

0

Second (and all other) "WHERE" produce "AND" statement

Last updated 9 years ago.
0

This condition:

(int)$textToSearch

Doesn't check if it's numeric or not. It firstly casts your string to integer and then to boolean. If your text is '123abc', it will be executed, because (int) '123abc' is 123 and (boolean) 123 is true. My favorite way to check if the string is TRUE INTEGER number is:

(string) (int) $value === (string) $value

There are some built-in PHP functions to check if string is numeric, but I found all of them wrong in some way. Checking just if it contains digits only is bad as well, because "01" is not a true integer.

0

Thank you very much kokokurak. Really helpful!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

heremiguel heremiguel Joined 29 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.