<?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 - itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</title>
	<atom:link href="https://www.itoffside.com/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99-laravel/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.itoffside.com</link>
	<description>ข้อมูลข่าวสารและเรื่องราวบนโลกของไอทีที่ ล้ำหน้า พร้อมสาระการเขียนโปรแกรมคอมพิวเตอร์</description>
	<lastBuildDate>Sun, 01 Jan 2023 09:52:29 +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 - itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</title>
	<link>https://www.itoffside.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Laravel &#8211; EP11 การใช้งาน Model</title>
		<link>https://www.itoffside.com/laravel-ep11-model/</link>
					<comments>https://www.itoffside.com/laravel-ep11-model/#respond</comments>
		
		<dc:creator><![CDATA[Onmobile]]></dc:creator>
		<pubDate>Mon, 13 Apr 2020 15:10:32 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[laravel model]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[php7]]></category>
		<category><![CDATA[สอน laravel]]></category>
		<category><![CDATA[สอน model laravel]]></category>
		<guid isPermaLink="false">https://www.itoffside.com/?p=2902</guid>

					<description><![CDATA[<p>สำหรับ Model ใน Laravel มีการใช้ Eloquent ORM ที่มาพร้อมกับ Laravel ซึ่งใช้งาน ActiveRecord ที่ทำให้เราเขียนโค๊ดเป็นระเบียบสวยงามและเรียบง่าย ในแต่ละตารางฐานข้อมูลมี &#8220;Model&#8221; ที่เป็นแบบจำลองให้เราสามารถทำงานกับฐานข้อมูล บนการเขียนโค๊ดได้ เปรียบเทียบง่ายๆเช่นเรามี ตาราง products, customer ...</p>
<p>The post <a href="https://www.itoffside.com/laravel-ep11-model/">Laravel – EP11 การใช้งาน Model</a> first appeared on <a href="https://www.itoffside.com">itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>สำหรับ <strong>Model</strong> ใน Laravel มีการใช้ <strong>Eloquent ORM</strong> ที่มาพร้อมกับ Laravel ซึ่งใช้งาน <strong>ActiveRecord</strong> ที่ทำให้เราเขียนโค๊ดเป็นระเบียบสวยงามและเรียบง่าย ในแต่ละตารางฐานข้อมูลมี &#8220;Model&#8221; ที่เป็นแบบจำลองให้เราสามารถทำงานกับฐานข้อมูล บนการเขียนโค๊ดได้ เปรียบเทียบง่ายๆเช่นเรามี ตาราง products, customer เราก็จะมี <span style="color: #333399;">Model Product.php, Customer.php</span> เป็นต้น<span id="more-2902"></span></p>
<p>ก่อนเริ่มสร้าง Model เราต้องแน่ใจก่อนว่าเรามีการเชื่อมต่อฐานข้อมูลแล้ว วิธีการตรวจสอบสามารถเข้าไปตั้งค่าที่ ไฟล์ .env ดังนี้</p><pre class="crayon-plain-tag">DB_DATABASE=myblog
DB_USERNAME=root
DB_PASSWORD=mypassword</pre><p>Model เวลาเราสร้างแล้วไฟล์จะไปอยู่ที่ App ซึ่งเราสามารถนำไปไว้ที่ไหนก็ได้ แต่ในตัวอย่าง และคำแนะนำของผมผมจะแนะนำให้เรานำไฟล์ Model ไปไว้ที่ App\Models และวิธีการสร้าง Model ให้เราเปิด Command line มาแล้ว ให้พิมพ์คำสั่ง ตามโค๊ดด้านล่าง</p><pre class="crayon-plain-tag">php artisan make:model Models/Flight</pre><p>เรามาเข้ามาดูไฟล์ที่เราสร้างในโฟล์เดอร์ <span style="color: #333399;">App/Models/Flight.php</span><br />
<img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3233" src="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-13-02.jpg" alt="" width="506" height="233" srcset="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-13-02.jpg 506w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-13-02-300x138.jpg 300w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-13-02-80x37.jpg 80w" sizes="(max-width: 506px) 100vw, 506px" /></p>
<p>Laravel artisan ยังมีความสามารถในการสร้างไฟล์ migration ได้อีกด้วย (สำหรับ migration คือการเขียนโค๊ด PHP สร้างตารางในฐานข้อมูล) โดยคำสั่งให้เรา เติมคำ &#8211;migration หรือ -m ต่อท้าย ลองดูตัวอย่างครับ</p><pre class="crayon-plain-tag">php artisan make:model Models/Flight -m</pre><p><img decoding="async" class="alignnone size-full wp-image-3234" src="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-17-20.jpg" alt="" width="598" height="101" srcset="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-17-20.jpg 598w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-17-20-300x51.jpg 300w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-17-20-80x14.jpg 80w" sizes="(max-width: 598px) 100vw, 598px" /></p>
<p>หลังจากใช้คำสั่งสร้าง Model พร้อม Migration แล้วไฟล์ Migration จะอยู่ในโฟล์เดอร์ <span style="color: #333399;">database/migrations</span></p>
<p>ให้เรากลับไปเปิดไฟล์ <span style="color: #333399;">XXX_XX_XX_XXXXX_create_flights_table.php</span> ในโฟล์เดอร์ <span style="color: #0000ff;">database/migrations</span> แล้วเพิ่มเติมโค๊ดลงไปแบบตัวอย่างด้านล่าง</p><pre class="crayon-plain-tag">&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string('name')-&gt;nuallable();
            $table-&gt;string('detail')-&gt;nuallable();
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('flights');
    }
}</pre><p>หลังจากนั้น run คำสั่ง</p><pre class="crayon-plain-tag">php artisan migrate</pre><p>ให้คำสั่งไป excute โค๊ดให้สร้างตาราง หลังจาก run แล้วเราจะได้ผลลัพท์คือได้ตาราง flights มา<br />
<img decoding="async" class="alignnone size-full wp-image-3236" src="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-41-50.jpg" alt="" width="578" height="178" srcset="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-41-50.jpg 578w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-41-50-300x92.jpg 300w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-41-50-80x25.jpg 80w" sizes="(max-width: 578px) 100vw, 578px" /></p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-3237" src="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-42-38.jpg" alt="" width="345" height="179" srcset="https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-42-38.jpg 345w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-42-38-300x156.jpg 300w, https://www.itoffside.com/wp-content/uploads/2018/11/2020-04-13_21-42-38-80x42.jpg 80w" sizes="auto, (max-width: 345px) 100vw, 345px" /></p>
<p>ต่อไปเราจะมาดู Model กันต่อว่ามีอะไรบ้าง</p>
<h3>Table Name</h3>
<p>เวลาเราสร้าง Model มานั้น Laravel จะ จดจำชื่อ Model กับ ชื่อตารางเป็นอันเดียวกันเช่น Model Flight ตารางในฐานข้อมูลเป็น flights แต่ที่นี้ถ้าชื่อตารางเป็นชื่ออื่นเช่น my_flights ละ ต้องทำแบบไหน วิธีการคือให้ใส่คำสั่ง <span style="color: #0000ff;">protected $table = &#8216;my_flights&#8217;;</span> ลงไปใน Model เดี่ยวมาดู โค๊ดแบบเต็มกัน</p><pre class="crayon-plain-tag">&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'my_flights';
}</pre><p></p>
<h3>Primary Key</h3>
<p>โดย Model จะจดจำ Primary key ของตารางเป็น id หากเราใช้ชื่อ flight_id เป็น Primary key สามารถเปลี่ยนได้ โดยเขียนคำสั่ง <span style="color: #0000ff;">protected $primaryKey = &#8216;flight_id&#8217;;</span></p><pre class="crayon-plain-tag">&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'flight_id';
}</pre><p></p>
<h3>Timestamps</h3>
<p>ค่าเริ่มต้นสำหรับ การบันทึกเวลาอัตโนมัติ ใน Model นั้นจะต้องมีคอลัมน์ created_at, updated_at แต่ในบางทีตารางเราไม่จำเป็นต้องมีการบันทึกเวลาไม่ต้องมี คอลัมน์เหล่านี้ ถ้าเราไม่ยกเลิกใช้งาน timestamps ใน Model เวลาเราบันทึกข้อมูล จะเกิด Error วิธีการแก้ไขนั้นให้เขียนคำสั่ง <span style="color: #0000ff;">public $timestamps = false;</span></p>
<p>และอีกทั้งคุณยังไม่จำเป็นต้องกำหนดคอลัมน์ให้เป็นชื่อ created_at, updated_at สามารถเปลี่ยนชื่อได้ โดยเขียนคำสั่ง&nbsp;<br />
<span style="color: #0000ff;">const CREATED_AT = &#8216;creation_date&#8217;;</span><br />
<span style="color: #0000ff;">const UPDATED_AT = &#8216;last_update&#8217;;</span></p><pre class="crayon-plain-tag">&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'last_update';
    public $timestamps = false;
}</pre><p></p>
<h3>Retrieving Models</h3>
<p>เมื่อเราสร้าง Model และ ตารางในฐานข้อมูลแล้ว เราก็พร้อมสำหรับการดึงข้อมูลที่ต้องการจากฐานข้อมูล โดยใช้ Eloquent model ในการทำ Query builder กับ Model ลองมาดูตัวอย่างโดยเขียน ไว้ใน Controller</p><pre class="crayon-plain-tag">&lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Flight;

class WelcomeController extends Controller
{
    public function index()
    {
        $flights = Flight::all();

        foreach ($flights as $flight) {
            echo $flight-&gt;name;
        }
    }
}</pre><p>และถ้าหากเราต้องการให้ข้อมูลเรียกเอาเฉพาะ Active=1 และ เรียงข้อมูลคอลัมน์ name จาก มากไปน้อย และดึงมาแค่ 10 รายการ สามารถเขียนได้ดังนี้</p><pre class="crayon-plain-tag">$flights = Flight::where('active', 1)
               -&gt;orderBy('name', 'desc')
               -&gt;take(10)
               -&gt;get();</pre><p>ตรงนี้ เราสามารถดูคำสั่งในการ query builder ได้ที่ https://laravel.com/docs/7.x/queries</p>
<h3>สรุป</h3>
<p>สำหรับ บทความ พื้นฐาน Model มีเพียงเท่านี้ครับ และมีภาคต่อของ Model และเนื้อหาที่เกี่ยวข้องด้วย คือ การทำ Migration และการทำ Relation Model&nbsp;</p>
<p>สำหรับเนื้อหาส่วนนี้โดยรวมพูดถึงวิธีการสร้าง Model และภาพรวมว่า มีขั้นตอนการดึงข้อมูล การปรับแต่ง Model อะไรบ้าง รายละเอียดในบทความนี้หวังว่าจะเป็นแนวทางในการพัฒนาระบบของผู้อ่าน นะครับ</p><p>The post <a href="https://www.itoffside.com/laravel-ep11-model/">Laravel – EP11 การใช้งาน Model</a> first appeared on <a href="https://www.itoffside.com">itOffside.com | บทความการเขียนโปรแกรม เรื่องราวข้อมูลเทคโนโลยี</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.itoffside.com/laravel-ep11-model/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
