<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>laravel-Eloquent-database - itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</title>
	<atom:link href="https://www.itoffside.com/tag/laravel-eloquent-database/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.itoffside.com</link>
	<description>ข้อมูลข่าวสารและเรื่องราวบนโลกของไอทีที่ ล้ำหน้า พร้อมสาระการเขียนโปรแกรมคอมพิวเตอร์</description>
	<lastBuildDate>Thu, 12 Jan 2023 14:35:58 +0000</lastBuildDate>
	<language>th</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>

<image>
	<url>https://www.itoffside.com/wp-content/uploads/2020/04/cropped-favicon-96x96-1-32x32.png</url>
	<title>laravel-Eloquent-database - itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</title>
	<link>https://www.itoffside.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Laravel &#8211; EP13 Database กับการใช้งาน Eloquent</title>
		<link>https://www.itoffside.com/laravel-ep13-database-eloquent/</link>
					<comments>https://www.itoffside.com/laravel-ep13-database-eloquent/#respond</comments>
		
		<dc:creator><![CDATA[Onmobile]]></dc:creator>
		<pubDate>Thu, 12 Jan 2023 14:34:19 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Eloquent]]></category>
		<category><![CDATA[Eloquent-database]]></category>
		<category><![CDATA[laravel-Eloquent]]></category>
		<category><![CDATA[laravel-Eloquent-database]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php 8]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[php7]]></category>
		<guid isPermaLink="false">https://www.itoffside.com/?p=2909</guid>

					<description><![CDATA[<p>Laravel ใช้ Eloquent ORM เป็นตัวช่วยในการเชื่อมต่อฐานข้อมูล โดย Eloquent จะมีการแปลงข้อมูลในฐานข้อมูลเป็นออบเจกต์ (Object) บนภาษา PHP ซึ่งช่วยให้นักพัฒนาสามารถเขียนโค้ดที่เชื่อมต่อฐานข้อมูลแบบง่ายขึ้น โดยไม่ต้องเขียน SQL เอง เพื่อใช้งาน Eloquent คุณสามารถสร้างคลาสที่สืบทอดจากคลาส Eloquent และปรับปรุ...</p>
<p>The post <a href="https://www.itoffside.com/laravel-ep13-database-eloquent/">Laravel – EP13 Database กับการใช้งาน Eloquent</a> first appeared on <a href="https://www.itoffside.com">itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</a>.</p>]]></description>
										<content:encoded><![CDATA[<p><strong>Laravel ใช้ Eloquent ORM</strong> เป็นตัวช่วยในการเชื่อมต่อฐานข้อมูล โดย <span style="text-decoration: underline;"><strong>Eloquent</strong> </span>จะมีการแปลงข้อมูลในฐานข้อมูลเป็นออบเจกต์ (Object) บนภาษา PHP ซึ่งช่วยให้นักพัฒนาสามารถเขียนโค้ดที่เชื่อมต่อฐานข้อมูลแบบง่ายขึ้น โดยไม่ต้องเขียน SQL เอง</p>
<p>เพื่อใช้งาน <span style="text-decoration: underline;">Eloquent</span> คุณสามารถสร้างคลาสที่สืบทอดจากคลาส <span style="text-decoration: underline;">Eloquent</span> และปรับปรุงข้อมูลตามความต้องการ คลาสนี้จะเรียกว่า &#8220;<strong><span style="text-decoration: underline;">Model</span></strong>&#8221; ซึ่งจะเป็นตัวแทนของตารางในฐานข้อมูลนั่นเอง<span id="more-2909"></span></p>
<p>หากต้องการสร้าง Model สำหรับตาราง &#8220;users&#8221; ของฐานข้อมูล สามารถสร้างไฟล์ &#8220;User.php&#8221; ภายในโฟลเดอร์ &#8220;<span style="color: #3366ff;">app/Models</span>&#8221; และสร้างไฟล์ User.php แล้วเขียนคลาสดังนี้</p><pre class="crayon-plain-tag">&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    //
}</pre><p>หลังจากนั้น ให้ไฟล์ใน Controller สามารถเรียกใช้งาน Eloquent เพื่อดึงข้อมูลจากตาราง &#8220;users&#8221; ของฐานข้อมูลได้ดังนี้</p><pre class="crayon-plain-tag">&lt;?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();
        return view('users.index', compact('users'));
    }
}</pre><p>จากตัวอย่างข้างต้น สามารถใช้งานฟังก์ชัน all เพื่อดึงข้อมูลทั้งหมดจากตาราง &#8220;users&#8221; และนำข้อมูลทั้งหมดนั้นไปแสดงผลบนหน้าจอผ่านทางเทมเพลท (view) ไฟล์<span style="color: #3366ff;"> resources/views/users/index.blade.php</span></p>
<p>น่าจะเป็นสิ่งที่มีประโยชน์มากสำหรับนักพัฒนาเว็บแอพพลิเคชัน เพราะมันช่วยให้สร้างโค้ดที่ลดข้อผิดพลาดลง โดยไม่ต้องเขียนคำส่ง SQL ของฐานข้อมูลขึ้นมาเอง นอกจากนี้ Eloquent ยังมีการสนับสนุนการใช้งานฟังก์ชันการค้นหาและการเรียงลำดับข้อมูล ทำให้คุณสามารถสร้างโค้ดที่มีประสิทธิภาพสูงขึ้นได้อีกด้วย</p>
<p>นอกจากนี้ Eloquent ยังมีการสนับสนุนการทำงานร่วมกับความสัมพันธ์ของข้อมูลระหว่างตารางต่าง ๆ หรือเรียกว่า Relation ซึ่งช่วยให้สามารถสร้างโค้ดที่สามารถเข้าถึงข้อมูลที่เกี่ยวข้องกันได้ง่ายขึ้น ตัวอย่างเช่น หากตาราง &#8220;users&#8221; มีความสัมพันธ์กับตาราง &#8220;orders&#8221; ซึ่งเป็น 1-N ดังนั้นสามารถสร้างฟังก์ชันที่เรียกว่า &#8220;orders&#8221; ใน Model ของ &#8220;User&#8221; เพื่อเข้าถึงข้อมูลการสั่งซื้อของผู้ใช้นั้นๆ ดังนี้ โดยให้เข้าไปแก้ไข code ที่ไฟล์ <span style="color: #3366ff;">app\Models\User.php</span></p><pre class="crayon-plain-tag">&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function orders()
    {
        return $this-&gt;hasMany(Order::class);
    }
}</pre><p>จากตัวอย่างข้างต้น สามารถเรียกใช้งานฟังก์ชัน orders เพื่อเข้าถึงข้อมูลการสั่งซื้อของผู้ใช้นั้นๆ ได้ดังนี้ ใน Controller</p><pre class="crayon-plain-tag">$user = User::find(1);
$orders = $user-&gt;orders;</pre><p>Eloquent ยังมีการสนับสนุนการทำงานร่วมกับความสัมพันธ์ของข้อมูลอื่น ๆ เช่น ความสัมพันธ์ 1-1, N-1 และ N-N เพื่ออธิบายความสัมพันธ์นี้ในรูปแบบของคลาส สามารถใช้คำสั่งต่าง ๆ ของ Eloquent เพื่อสร้างความสัมพันธ์นี้ได้ ตัวอย่างเช่น การสร้างความสัมพันธ์ 1-1 ระหว่าง Model ของ &#8220;User&#8221; กับ Model ของ &#8220;Profile&#8221; คุณ สามารถเขียนโค้ดดังนี้ ที่ไฟล์ <span style="color: #3366ff;">app\Models\User.php</span></p><pre class="crayon-plain-tag">&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function profile()
    {
        return $this-&gt;hasOne(Profile::class);
    }
}</pre><p>จากตัวอย่างข้างต้น สามารถเรียกใช้งานฟังก์ชัน profile เพื่อเข้าถึงข้อมูลของโปรไฟล์ของผู้ใช้นั้นๆ ได้ดังนี้ใน <strong>Controller</strong></p><pre class="crayon-plain-tag">$user = User::find(1);
$profile = $user-&gt;profile;</pre><p>Eloquent ยังสามารถช่วยให้คุณสร้างความสัมพันธ์ของข้อมูลแบบ N-N ได้ ด้วยการใช้คำสั่ง belongsToMany แทน ตัวอย่างเช่น การสร้างความสัมพันธ์ N-N ระหว่าง Model ของ &#8220;User&#8221; กับ Model ของ &#8220;Role&#8221; สามารถเขียนโค้ดดังนี้</p><pre class="crayon-plain-tag">&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function roles()
    {
        return $this-&gt;belongsToMany(Role::class);
    }
}</pre><p>จากตัวอย่างข้างต้น สามารถเรียกใช้งานฟังก์ชัน roles เพื่อเข้าถึงข้อมูลของบทบาทของผู้ใช้นั้นๆ ได้ดังนี้ ใน <strong>Controller</strong></p><pre class="crayon-plain-tag">$user = User::find(1);
$roles = $user-&gt;roles;</pre><p></p><pre class="crayon-plain-tag">$user = User::find(1);
$roles = $user-&gt;roles;</pre><p>นอกจากนี้ ยังสามารถเรียกใช้งานฟังก์ชันต่าง ๆ ของ Eloquent เพื่อจัดการข้อมูลของตารางอื่นๆ ได้อย่าง save, update, delete หรือการเรียกดูข้อมูลแบบกำหนดเงื่อนไขได้ ตัวอย่างเช่น การอัพเดทข้อมูลของผู้ใช้ที่มี ID เท่ากับ 1 สามารถเขียนโค้ดดังนี้</p><pre class="crayon-plain-tag">$user = User::find(1);
$user-&gt;name = 'John';
$user-&gt;save();</pre><p>นอกจากนี้ ยังสามารถเรียกใช้งานคำสั่ง update ของ Eloquent เพื่ออัพเดทข้อมูลทีละหลายฟิลด์พร้อมๆ กันได้ เช่น</p><pre class="crayon-plain-tag">User::where('id', 1)-&gt;update([
    'name' =&gt; 'John',
    'email' =&gt; 'john@example.com',
]);</pre><p>นอกจากนี้ยังสามารถเรียกใช้งานคำสั่ง delete ของ Eloquent เพื่อลบข้อมูลทีละหลายแถวพร้อมๆ กันได้ เช่น</p><pre class="crayon-plain-tag">User::where('id', '&gt;', 1)-&gt;delete();</pre><p>เพื่อที่จะเข้าถึงข้อมูลแบบกำหนดเงื่อนไข สามารถใช้ฟังก์ชัน where ของ Eloquent ซึ่งจะสร้างคำสั่ง WHERE ของ SQL สำหรับการค้นหาข้อมูลได้ ตัวอย่างเช่น การเรียกดูข้อมูลของผู้ใช้ที่มีอีเมลล์ลงทะเบียนเป็น &#8220;john@example.com&#8221; สามารถเขียนโค้ดดังนี้</p><pre class="crayon-plain-tag">$users = User::where('email', 'john@example.com')-&gt;get();</pre><p>สามารถปรับปรุงฟังก์ชัน where เพิ่มเติมโดยฟังก์ชันอื่น ๆ เช่น orWhere หรือ whereIn ซึ่งจะช่วยค้นหาข้อมูลตามเงื่อนไขที่กำหนดเพิ่มเติม ตัวอย่างเช่น การเรียกดูข้อมูลของผู้ใช้ที่มีอีเมลล์ลงทะเบียนเป็น &#8220;john@example.com&#8221; หรือ &#8220;jane@example.com&#8221; สามารถเขียนโค้ดดังนี้</p><pre class="crayon-plain-tag">$users = User::where('email', 'john@example.com')
    -&gt;orWhere('email', 'jane@example.com')
    -&gt;get();</pre><p>นอกจากนี้ ยังสามารถใช้ฟังก์ชัน whereIn เพื่อค้นหาข้อมูลของผู้ใช้ที่มีอีเมลล์ลงทะเบียนอยู่ในรายการที่กำหนด ตัวอย่างเช่น การเรียกดูข้อมูลของผู้ใช้ที่มีอีเมลล์ลงทะเบียนเป็น &#8220;john@example.com&#8221; หรือ &#8220;jane@example.com&#8221; สามารถเขียนโค้ดดังนี้</p><pre class="crayon-plain-tag">$users = User::whereIn('email', ['john@example.com', 'jane@example.com'])-&gt;get();</pre><p>Eloquent ยังมีฟังก์ชันอื่น ๆ ที่สามารถใช้งานเพื่อค้นหาข้อมูลแบบกำหนดเงื่อนไขได้ เช่น whereBetween หรือ whereNotIn สามารถศึกษาเพิ่มเติมได้จากเอกสารของ <strong>Laravel</strong></p>
<p>การใช้งาน <strong>Eloquent</strong> นอกจากทำให้นักพัฒนาสะดวกสบายยังทำให้งานโปรเจ็คต่างๆเสร็จอย่างรวดเร็ว อีกทั้งยัง code ยังเป็นระเบียนและสามารถต่อยอดแก้ไขได้ในอนาคต ผู้เขียนหวังว่าบทความนี้จะเป็นประโยชน์ต่อผู้อ่าน หากมีข้อสงสัยตรงไหนสอบถามใน Comment ได้เลยครับ</p>
<p>ขอบคุณครับ</p><p>The post <a href="https://www.itoffside.com/laravel-ep13-database-eloquent/">Laravel – EP13 Database กับการใช้งาน Eloquent</a> first appeared on <a href="https://www.itoffside.com">itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.itoffside.com/laravel-ep13-database-eloquent/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
