Laravel: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
mNo edit summary
 
Line 16: Line 16:
$query = DB::table("articles")
$query = DB::table("articles")
         ->select("content")
         ->select("content")
         ->whereRaw("content LIKE ? ", ['%' . $search_keyword . '%'])
         ->whereRaw("content LIKE ? ", ["%$search_keyword%"])
</pre>
</pre>


Line 23: Line 23:
$query = DB::table("articles")
$query = DB::table("articles")
         ->select("content")
         ->select("content")
         ->where("content", "LIKE", '%' . $search_keyword . '%')
         ->where("content", "LIKE", "%$search_keyword%")
</pre>
</pre>



Latest revision as of 11:32, 29 August 2019

Laravel - The PHP Framework For Web Artisans (繁體中文文件: Laravel - 為網頁藝術家創造的 PHP 框架)

SQL 查詢語法 Where 變數部分使用問號[edit]

Icon_exclaim.gif "Raw statements will be injected into the query as strings, so you should be extremely careful to not create SQL injection vulnerabilities."[1]

原始 SQL 查詢語法

SELECT `content`
FROM `articles`
WHERE `content` LIKE "%$search_keyword%"

使用 Laravel whereRaworWhereRaw,範例代碼

$query = DB::table("articles")
        ->select("content")
        ->whereRaw("content LIKE ? ", ["%$search_keyword%"])

另一種 Laravel 寫法

$query = DB::table("articles")
        ->select("content")
        ->where("content", "LIKE", "%$search_keyword%")

相關資料

References[edit]