Inserting a variable in a raw sql query Laravel

后端 未结 3 486
既然无缘
既然无缘 2021-01-05 01:33

I am inside a function in a controller.

So from the Form, I get a value for a variable, say:

$x = \"whatever\";

Then I need to embe

相关标签:
3条回答
  • 2021-01-05 02:14

    This is one example for you to insert variable in a raw sql laravel

            $query_result = Event::select(
                DB::raw('(CASE WHEN status = "draft" THEN "draft" 
                WHEN events.end_time <= \''.$now.'\' THEN "closed"
                ELSE "available"
                END) AS status'))
                ->orderBy('status')
                ->get();
    
    0 讨论(0)
  • 2021-01-05 02:15

    Regarding this tutorial

    $results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = :somevariable"), array(
       'somevariable' => $someVariable,
     ));
    
    0 讨论(0)
  • This appears to be a simple PHP variable interpolation issue.

    DB::raw() wants literally raw SQL. So there are a couple of issues that need to be fixed in the SQL string you are passing.

    1. PHP Variable interpolation (injecting variables into a string) only happens if you use double quotes around the string. With single quotes it becomes a string constant.
    2. If Author is a char/varchar, then SQL syntax requires quotes around the string in your raw SQL statement. Query builders typically take care of these issues for you, but you are going around them.

    So the "fixed" version of this would be:

    $x = "whatever";
    $results = DB::select(DB::raw("SELECT 
                           t.id, t.AvgStyle, r.RateDesc
                       FROM (
                           SELECT
                               p.id, ROUND(AVG(s.Value)) AS AvgStyle
                           FROM posts p
    
                           INNER JOIN styles s
                               ON s.post_id = p.id
                           WHERE author = '$x'    
                           GROUP BY p.id
                       ) t
                       INNER JOIN rates r
                           ON r.digit = t.AvgStyle"
                       ));
    

    Like all interpolation, this opens you up to the possibility of SQL injection if the variable being interpolated comes from user input. From the original question it is unclear whether this is a problem.

    DB::select() has an option that allows you to pass an array of parameters that is inherently safe from SQL injection. In that case the solution would be:

    $x = "whatever";
    $results = DB::select(DB::raw("SELECT 
                           t.id, t.AvgStyle, r.RateDesc
                       FROM (
                           SELECT
                               p.id, ROUND(AVG(s.Value)) AS AvgStyle
                           FROM posts p
    
                           INNER JOIN styles s
                               ON s.post_id = p.id
                           WHERE author = :author
                           GROUP BY p.id
                       ) t
                       INNER JOIN rates r
                           ON r.digit = t.AvgStyle"
                       ),
                           array('author' => $x)
                       );
    
    0 讨论(0)
提交回复
热议问题