-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.html
94 lines (89 loc) · 32.5 KB
/
model.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Model | Pinatra</title>
<meta name="description" content="A PHP verison of Sinatra: a DSL for quickly creating web applications in PHP with minimal effort.">
<link rel="preload" href="/assets/css/0.styles.73742341.css" as="style"><link rel="preload" href="/assets/js/app.e63a7f08.js" as="script"><link rel="preload" href="/assets/js/9.b089bacd.js" as="script"><link rel="prefetch" href="/assets/js/2.9834f0cc.js"><link rel="prefetch" href="/assets/js/3.fca8f696.js"><link rel="prefetch" href="/assets/js/4.340762b6.js"><link rel="prefetch" href="/assets/js/5.0d4bea33.js"><link rel="prefetch" href="/assets/js/6.1a48edea.js"><link rel="prefetch" href="/assets/js/7.35209b6c.js"><link rel="prefetch" href="/assets/js/8.608908b9.js"><link rel="prefetch" href="/assets/js/10.10bcbbf7.js"><link rel="prefetch" href="/assets/js/11.f3eb16c6.js"><link rel="prefetch" href="/assets/js/12.5fc13162.js"><link rel="prefetch" href="/assets/js/13.858358dd.js"><link rel="prefetch" href="/assets/js/14.6911e67f.js"><link rel="prefetch" href="/assets/js/15.2e2bc03a.js"><link rel="prefetch" href="/assets/js/16.0077a43e.js"><link rel="prefetch" href="/assets/js/17.c5a37954.js"><link rel="prefetch" href="/assets/js/18.ae59c8fa.js"><link rel="prefetch" href="/assets/js/19.b46aa2f8.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.41d79f37.js">
<link rel="stylesheet" href="/assets/css/0.styles.73742341.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Pinatra</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://github.com/Pinatra/Pinatra" target="_blank" rel="noopener noreferrer" class="nav-link external">
Source
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/model.html" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/model.html" class="nav-link">简体中文</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="https://github.com/Pinatra/Pinatra" target="_blank" rel="noopener noreferrer" class="nav-link external">
Source
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/model.html" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/model.html" class="nav-link">简体中文</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>Guide</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/" class="sidebar-link">Introduction</a></li><li><a href="/install.html" class="sidebar-link">Installation</a></li><li><a href="/routing.html" class="sidebar-link">Routing</a></li><li><a href="/controller.html" class="sidebar-link">Controller</a></li><li><a href="/view.html" class="sidebar-link">Views</a></li><li><a href="/model.html" class="active sidebar-link">Model</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/model.html#features" class="sidebar-link">Features</a></li><li class="sidebar-sub-header"><a href="/model.html#start-using" class="sidebar-link">Start Using</a></li><li class="sidebar-sub-header"><a href="/model.html#basic-usage-example" class="sidebar-link">Basic usage example</a></li><li class="sidebar-sub-header"><a href="/model.html#documentation-resources" class="sidebar-link">Documentation Resources</a></li><li class="sidebar-sub-header"><a href="/model.html#execution-an-sql-directly" class="sidebar-link">Execution an SQL Directly</a></li></ul></li><li><a href="/kernel.html" class="sidebar-link">Kernel details</a></li><li><a href="/benchmark.html" class="sidebar-link">Benchmark</a></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="model"><a href="#model" aria-hidden="true" class="header-anchor">#</a> Model</h1> <p>Pinatra uses the excellent ORM in the Laravel framework: <a href="https://github.com/Illuminate/Database/" target="_blank" rel="noopener noreferrer">Eloquent<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> as the default ORM.</p> <h2 id="features"><a href="#features" aria-hidden="true" class="header-anchor">#</a> Features</h2> <p>The Model base class is <a href="https://github.com/Pinatra/Pinatra/blob/master/src/Model/Model.php" target="_blank" rel="noopener noreferrer">\Pinatra\Model\Model<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> with the following characteristics:</p> <ol><li>Lazy loading, no initialization when not using Model, reducing unnecessary resource consumption</li> <li>Eloquent is powerful and of high quality, it is the most important reason for Laravel's strength.</li> <li>The powerful Model base class compresses the MVC architecture into an VC architecture, which greatly reduces the middle-level functions and reduces the editing of the same piece of code when multi-person collaboration. This is a huge improvement in software engineering. This is my deep experience after using Laravel in the past few years and the system is getting bigger and bigger.</li></ol> <h2 id="start-using"><a href="#start-using" aria-hidden="true" class="header-anchor">#</a> Start Using</h2> <p>Suppose we want to manipulate the articles table. Here is the specific process.</p> <h3 id="database-configuration"><a href="#database-configuration" aria-hidden="true" class="header-anchor">#</a> Database Configuration</h3> <p>Add the following statement to the appropriate location in <code>index.php</code> to complete the database configuration:</p> <div class="language-php extra-class"><pre class="language-php"><code>\<span class="token package">Pinatra<span class="token punctuation">\</span>Model<span class="token punctuation">\</span>Model</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token single-quoted-string string">'driver'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'mysql'</span><span class="token punctuation">,</span>
<span class="token single-quoted-string string">'host'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'localhost'</span><span class="token punctuation">,</span>
<span class="token single-quoted-string string">'database'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'database'</span><span class="token punctuation">,</span>
<span class="token single-quoted-string string">'username'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'root'</span><span class="token punctuation">,</span>
<span class="token single-quoted-string string">'password'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'password'</span><span class="token punctuation">,</span>
<span class="token single-quoted-string string">'charset'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'utf8'</span><span class="token punctuation">,</span>
<span class="token single-quoted-string string">'collation'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">'utf8_unicode_ci'</span><span class="token punctuation">,</span>
<span class="token single-quoted-string string">'prefix'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token single-quoted-string string">''</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="create-folder"><a href="#create-folder" aria-hidden="true" class="header-anchor">#</a> Create Folder</h3> <p>Create a new models folder at public level and add it to composer to load automatically:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token property">"autoload"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"psr-4"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"Model\\"</span><span class="token operator">:</span> <span class="token string">"models/"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Then run <code>composer dump-autoload</code>.</p> <h3 id="create-model"><a href="#create-model" aria-hidden="true" class="header-anchor">#</a> Create Model</h3> <p>Create a new <code>models/Article.php</code> with the following contents:</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">Model</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">Article</span> <span class="token keyword">extends</span> <span class="token class-name"><span class="token punctuation">\</span>Pinatra<span class="token punctuation">\</span>Model<span class="token punctuation">\</span>Model</span>
<span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</span></code></pre></div><h3 id="creating-a-table-in-a-new-database"><a href="#creating-a-table-in-a-new-database" aria-hidden="true" class="header-anchor">#</a> Creating a table in a new database</h3> <p>Article defaults to the articles table, and ArticleItem corresponds to the article_itmes table, which converts the hump method into a serpentine shape, and the last word needs to be converted into English plural (very magical).</p> <p>If you want to customize the name of the data table, it is also very simple:</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">class</span> <span class="token class-name">Article</span> <span class="token keyword">extends</span> <span class="token class-name"><span class="token punctuation">\</span>Pinatra<span class="token punctuation">\</span>Model<span class="token punctuation">\</span>Model</span>
<span class="token punctuation">{</span>
<span class="token keyword">protected</span> <span class="token variable">$table</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'my_articles'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>The specific construction process should be completed by yourself.</p> <h3 id="using-model"><a href="#using-model" aria-hidden="true" class="header-anchor">#</a> Using Model</h3> <h4 id="direct-use-in-routing-closure-callbacks"><a href="#direct-use-in-routing-closure-callbacks" aria-hidden="true" class="header-anchor">#</a> Direct use in routing closure callbacks</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token function">get</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'hi'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> \<span class="token package">Model<span class="token punctuation">\</span>Article</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="using-in-controller"><a href="#using-in-controller" aria-hidden="true" class="header-anchor">#</a> Using in Controller</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Pinatra<span class="token punctuation">\</span>Model<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">HomeController</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">home</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> \<span class="token package">Model<span class="token punctuation">\</span>Article</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></code></pre></div><h2 id="basic-usage-example"><a href="#basic-usage-example" aria-hidden="true" class="header-anchor">#</a> Basic usage example</h2> <h3 id="found-an-article-with-an-id-of-2-to-print-its-title"><a href="#found-an-article-with-an-id-of-2-to-print-its-title" aria-hidden="true" class="header-anchor">#</a> Found an article with an id of 2 to print its title</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$article</span> <span class="token operator">=</span> Article<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$article</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">title</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="find-the-article-titled-i-am-the-title-and-print-the-id"><a href="#find-the-article-titled-i-am-the-title-and-print-the-id" aria-hidden="true" class="header-anchor">#</a> Find the article titled "I am the title" and print the id</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$article</span> <span class="token operator">=</span> Article<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'title'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'I am the title'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$article</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="query-all-articles-and-cycle-through-all-the-titles"><a href="#query-all-articles-and-cycle-through-all-the-titles" aria-hidden="true" class="header-anchor">#</a> Query all articles and cycle through all the titles</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// The $articles obtained here is a collection of objects </span>
<span class="token comment">// that can be followed by a '->toArray()' to become a </span>
<span class="token comment">// multidimensional array.</span>
<span class="token variable">$articles</span> <span class="token operator">=</span> Article<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$articles</span> <span class="token keyword">as</span> <span class="token variable">$article</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token variable">$article</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">title</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="find-all-articles-with-id-between-10-and-20-and-print-all-the-titles"><a href="#find-all-articles-with-id-between-10-and-20-and-print-all-the-titles" aria-hidden="true" class="header-anchor">#</a> Find all articles with id between 10 and 20 and print all the titles</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$articles</span> <span class="token operator">=</span> Article<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'id'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'>'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'id'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'<'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$articles</span> <span class="token keyword">as</span> <span class="token variable">$article</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token variable">$article</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">title</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="query-all-articles-and-cycle-out-all-the-titles-sorted-by-updated-at-in-reverse-order"><a href="#query-all-articles-and-cycle-out-all-the-titles-sorted-by-updated-at-in-reverse-order" aria-hidden="true" class="header-anchor">#</a> Query all articles and cycle out all the titles, sorted by updated_at in reverse order</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$articles</span> <span class="token operator">=</span> Article<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'id'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'>'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'id'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'<'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'updated_at'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'desc'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$articles</span> <span class="token keyword">as</span> <span class="token variable">$article</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token variable">$article</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">title</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="documentation-resources"><a href="#documentation-resources" aria-hidden="true" class="header-anchor">#</a> Documentation Resources</h2> <p><a href="https://laravel.com/docs/5.7/eloquent" target="_blank" rel="noopener noreferrer">Eloquent Documentation<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="execution-an-sql-directly"><a href="#execution-an-sql-directly" aria-hidden="true" class="header-anchor">#</a> Execution an SQL Directly</h2> <h3 id="select-query"><a href="#select-query" aria-hidden="true" class="header-anchor">#</a> Select Query</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token constant">DB</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'select * from users where active = ?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="using-named-bindings"><a href="#using-named-bindings" aria-hidden="true" class="header-anchor">#</a> Using named bindings</h4> <p>In addition to using <code>?</code> to represent parameter bindings, you can also use a named binding to execute a query:</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token constant">DB</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'select * from users where id = :id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token single-quoted-string string">'id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="insert"><a href="#insert" aria-hidden="true" class="header-anchor">#</a> Insert</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token constant">DB</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'insert into users (id, name) values (?, ?)'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="update"><a href="#update" aria-hidden="true" class="header-anchor">#</a> Update</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$affected</span> <span class="token operator">=</span> <span class="token constant">DB</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'update users set votes = 100 where name = ?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token single-quoted-string string">'John'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="delete"><a href="#delete" aria-hidden="true" class="header-anchor">#</a> Delete</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$deleted</span> <span class="token operator">=</span> <span class="token constant">DB</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'delete from users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="running-ordinary-statements"><a href="#running-ordinary-statements" aria-hidden="true" class="header-anchor">#</a> Running ordinary statements</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token constant">DB</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">statement</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'drop table users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="more-documentation-about-db"><a href="#more-documentation-about-db" aria-hidden="true" class="header-anchor">#</a> More Documentation about DB</h3> <ul><li><a href="https://laravel.com/docs/5.7/database#running-queries" target="_blank" rel="noopener noreferrer">Running Raw SQL Queries<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li> <li><a href="https://laravel.com/docs/5.7/database#database-transactions" target="_blank" rel="noopener noreferrer">Database Transactions<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div> <div class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">4/22/2019, 12:03:41 PM</span></div></div> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/view.html" class="prev">
Views
</a></span> <span class="next"><a href="/kernel.html">
Kernel details
</a>
→
</span></p></div> </div> <!----></div></div>
<script src="/assets/js/9.b089bacd.js" defer></script><script src="/assets/js/app.e63a7f08.js" defer></script>
</body>
</html>