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();
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 !== '')
Thank you, the reason to cast it's to detect an integer and query only the id fields. Thank you for your response.
Second (and all other) "WHERE" produce "AND" statement
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community