Skip to content
CodingThailand's Blog
CodingThailand's Blog

by โค้ชเอก

  • Home
  • About Me
  • CodingThailand.com
CodingThailand's Blog

by โค้ชเอก

สรุป 39 คำสั่ง Laravel 5.5 ที่ใช้งานบ่อย

22/11/2017

laravel55

สรุป 39 คำสั่ง! Laravel 5.5 ที่ใช้งานบ่อย
———–

1. แสดงผลตัวแปรต่างๆที่ไปที่ view
view(‘task.index’)->with(‘tasks’, Task::all());
หรือ
view(‘task.index’,[‘tasks’, Task::all()]);


2. route cache

php artisan route:cache

3. ล้าง route cache
php artisan route:clear

4. สร้าง csrf tokens field ให้กับฟอร์ม
{{ csrf_field(); }}

5. คำสั่งเกี่ยวกับการ Redirects

return redirect()->to(‘login’);

หรือ
return redirect(‘login’);

6. route redirect เช่น
return redirect()->route(‘home.index’);
return redirect()->route(‘home.show’,[‘id’, 99]);

7. redirect back() ใช้
redirect()->back();
หรือเขียนย่อๆ แค่นี้
back();

8. redirect ไปที่ route ที่ชื่อว่า home
home();

9. refresh หน้า
refresh();

10. redirect โดยใช้ action() เช่น
redirect()->action(‘ชื่อController@ชื่อmethod’);

11. สร้าง flash data session
redirect()->with([‘error’=>true,’message’=>’Whoops!’]);

12. aborting the request
abort(403,’คุณไม่มีสิทธิ์ใช้งานส่วนนี้’);

13. return json
return response()->json(User::all());

14. ดาวน์โหลดไฟล์
return response()->download(‘file1.pdf’,’file2.pdf’);
หรือถ้าต้องการแสดงที่ browser ก็ใช้
return response()->file(‘file1.pdf’);

15. รับ input ทั้งหมดจาก request
$request->all();

16.รับ input ยกเว้นบางตัวใช้ except
$request->except(‘_token’);

17. รับ input เฉพาะที่ต้องการใช้ only
$request->only([‘firstname’,’email’]);

18. ใช้ has จะ return false ถ้ามีตัวแปร และว่าง
if ($request->has(‘file’)) {

}

19. จะ return true ถ้ามีตัวแปร และว่าง
if ($request->exists(’email’)) {

}

20. รับ request ทีละฟิลด์
$request->input(’email’)

21. ถ้าเป็น JSON Input ก็ใช้เหมือนกัน อ้างจุดไปที่ object

22. Accessors = getting data ของ Model

23. Mutators = setting data ของ Model

24. หากอยากซ่อนบางฟิลด์ ก็กำหนดที่ Model นั้นๆ ($hidden) เช่น

class Contact extends Model {

public $hidden = [‘password’,’email’];

หรือ เลือกแสดงบางฟิดล์ก็ใช้ ($visible) เช่น

public $visible = [‘name’,’gpa’];
}

25. เข้าถึงข้อมูลของ user โดยใช้ request เช่นอยากได้อีเมล์ ก็เขียนง่ายๆ ตามนี้
$request->user()->email
หรือเขียนที่ view ก็ได้ เช่น
ยินดีต้อนรับคุณ {{ auth()->user()->name }}

26. ตั้งชื่อให้กับ route เพื่อง่ายต่อการเรียกใช้งาน โดยระบุ ->name(‘ชื่อ route’) เช่น
Route::get(‘/home’, ‘HomeController@index’)->name(‘home’);

27. config cache ใช้คำสั่ง
php artisan config:cache

28. ล้าง config cache ใช้คำสั่ง
php artisan config:clear

29. ล้าง application cache
php artisan cache:clear

30. Compiling Assets (Laravel Mix)
ติดตั้ง Dependencies ใช้คำสั่ง npm install
รัน Laravel Mix ใช้คำสั่ง npm run dev หรือ npm run watch
หรือหากต้องการรันเพื่อ production ก็ใช้คำสั่ง npm run production

31. ดูว่า Laravel เตรียม frontend preset อะไรให้เราบ้างใช้คำสั่ง (ปกติก็มี bootstrap, vue, react, none)
php artisan preset –help

32. สร้างระบบ Authentication ใช้คำสั่ง (มีระบบล็อกอินมาให้เลย)
php artisan make:auth

33. แสดง route ทั้งหมดของ app เรา
php artisan route:list

34. คำสั่งสำหรับลบตารางทั้งหมด และสั่ง migrate ใหม่อีกครั้ง
php artisan migrate:fresh

35. คำสั่งแบ่งหน้า ใช้
$persons = Person::paginate(20);
หรือ
$persons = Person::simplePaginate(15);

36. ตัวอย่างการทำ Validation (เขียนที่ controller)
$request->validate([

‘title’ => ‘required’,

‘price’ => ‘required|numeric’,

‘image’ => ‘mimes:jpeg,jpg,png’
],[

‘title.required’ => ‘กรุณากรอกชื่อสินค้าด้วย’,

price.required’ => ‘กรุณากรอกราคา’,

‘price.numeric’ => ‘กรุณากรอกราคาเป็นตัวเลขเท่านั้น’,

‘image.mimes’ => ‘ไฟล์ที่เลือกต้องนามสกุล jpeg, jpg, png เท่านั้น’

]);

 

37. แสดงวันที่และเวลาปัจจุบัน ใช้คำสั่ง now() หรือ วันที่อย่างเดียวใช้ today() เช่น
{{ now() }}
{{ today() }}

 

38. ใช้ Bcrypt เพื่อ hash รหัสผ่าน เช่น
$password = bcrypt(‘1234’);

 

39. เรียกค่า config จากไฟล์ .env ใช้ config() แต่ตอนอ้างถึงใช้เครื่องหมายจุด แทน _ (underscore) เช่น
$value = config(‘app.timezone’);

 

สุดท้ายหากสนใจเขียน Laravel 5.5 แนะนำ e-Book เล่มนี้ครับ

http://www.codingthailand.com/laravel55ebook/
ขอบคุณครับ

โค้ชเอก
Coding Mentor

Views: 11260

Laravel 5

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Posts ล่าสุด

  • การใช้งาน Prefetching ใน Next.js
  • 14 ข้อคิด เพื่อการเป็น Developer ที่ดีขึ้น จาก “Lee Robinson”
  • เคยเจอ “TypeError: Cannot read property ‘x’ of undefined” หรือเปล่า
  • บันทึกการเขียนเว็บไซต์ใหม่ในรอบ 10 ปี ย้ายมาใช้ Next.js
  • ทำไมการจัดการ Error ใน JavaScript ถึงเป็นเรื่องท้าทาย?

Recent Comments

    หมวดหมู่

    • .NET
    • AI
    • Android
    • Angular
    • Angular 2
    • Coding
    • CSS
    • Database
    • Editor
    • Flutter
    • Git
    • HTML5
    • Ionic 2
    • Ionic 4
    • Ionic Framwork
    • JavaScript
    • Laravel
    • Laravel 5
    • Next.js
    • Node.js
    • PHP
    • PHP 7
    • Plugins
    • React
    • React Native
    • Template
    • Tools
    • TypeScript
    • UI
    • Uncategorized
    • Vue.js
    • XAMPP
    • Yii
    • คอร์สเรียน
    • แรงบันดาลใจ

    Archives

    • July 2025
    • April 2025
    • November 2024
    • October 2024
    • April 2020
    • February 2020
    • August 2019
    • September 2018
    • August 2018
    • February 2018
    • November 2017
    • October 2017
    • August 2017
    • July 2017
    • April 2017
    • October 2016
    • August 2016
    • May 2016

    Tags

    .NET android Angular Angular 2 Atom Coding Coding Standard CSS CSS 3 Datepicker Express.js extensions Git HTML HTML5 Ionic2 JavaScript Laravel5 laravel 5.5 MariaDB Material Design MySQL Node.js npm PHP PHP7 plugins PouchDB recaptcha Restful sail.js template typescript typscript XAMPP Yii2

    ผู้เยี่ยมชม

    • 0
    • 1,818,196
    • 547,569
    • 11,260
    ©2025 CodingThailand's Blog | WordPress Theme by SuperbThemes