0

Actually the scenario is my queries are running on the basis of if condition. In first scenario

if ($a != "") {
    $getData = DB::table('students')->where([
        ['code', '=', '1'],
        ['class', '!=', 'General'],
        ['gender', '=', 'm']
    ]);
}

> //second scenario

if ($b != '') {
    $queryData = $getData->where(ST_Distancesphere(geom, ST_SetSRID(ST_MakePoint($longt, $latt), 4326)), '<', $b)->get();
} else {
    $queryData = $getData->get();
}
return $queryData;

in first scenario query is working fine but when $b is not equal to blank then where condition is not working

1
  • 1
    Can you please tell what error is occured? Commented Jan 20, 2020 at 5:22

2 Answers 2

1

It seems you want to use ST_Distancesphere method, you need to use the raw sql. So if you use whereRaw() like this, and set binding for preventing SQL Injection:

$queryData=$getData->whereRaw("ST_Distancesphere(geom, ST_SetSRID(ST_MakePoint(:lng,:lat), 4326)) < :b", ["lng" => $longt, "lat" => $latt, "b" => $b])
                   ->get();

However, you have different bindings' way before this query,

DB::table('students')->where([['code', '=', '1'],['class', '!=', 'General'],['gender','=', 'm']])

Laravel will get Invalid parameter number error.

So I think you need to change the previous bindings, make them all use same bindings' way:

if($a != "") {
    $getData = DB::table('students')
               ->whereRaw("code = :code AND class != :class AND gender = :gender", ["code" => 1, "class" => "General", "gender" => "m"]);
}

if($b != '') {
    $queryData=$getData->whereRaw(" ST_Distancesphere(geom,ST_SetSRID(ST_MakePoint(:longt, :latt), 4326)) < :b", ["longt" => $longt, "latt" => $latt, "b" => $b])->get();
} else {
    $queryData=$getData->get();
}
return $queryData;
Sign up to request clarification or add additional context in comments.

9 Comments

the second query did not send any response or any error.
My ajax response send me 200 but query didn't send any response or any error when condition is $b !=''
When I am trying to print the query to add ->toSQL() in that case its not printing anything
->toSql() need to before ->get(); and do u try the raw sql in mysql, and did it return records?
actually query is not printing so how i can check in mysql
|
1

follow official documentation https://laravel.com/docs/5.8/queries#conditional-clauses

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.