Menu

Showing posts with label blade. Show all posts
Showing posts with label blade. Show all posts

Thursday, December 26, 2019

[Laravel - Blade Template] การใช้งาน Loops ประเภทต่างๆ
[Laravel - Blade Template] How to Use Each Loop Types

For Loop

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor

ForEach Loop

@foreach ($users as $user)
    This is user {{ $user->name }}
@endforeach

ForElse Loop - ForEach with handling for empty inputs

@forelse ($users as $user)
    This is user {{ $user->name }}
@empty
    No users
@endforelse

[Laravel - Blade Template] การตรวจสอบเงื่อนไขใน Blade Template
[Laravel - Blade Template] How to Use Conditional Statement

If Statement

@if (count($record) > 0)

@elseif (count($record) < 0)

@else

@endif

Unless Statement

@unless (count($record)< 0)

@endunless

[Laravel - Blade Template] การตรวจสอบการเข้าใช้งานและการตรวจสอบสิทธิ์การใช้งาน
[Laravel - Blade Template] Authentication and Authorization

การตรวจสอบการเข้าใช้งาน 

มี 2 แบบดังนี้
  • ตรวจสอบว่า user ได้เข้าสู่ระบบแล้ว
  • @auth
    
    @else
    
    @endauth
    
  • ตรวจสอบว่า user ยังไม่ได้เข้าสู่ระบบแล้ว
  • @guest
    
    @else
    
    @endguest
    

[Laravel - Blade Template] การสร้าง Layout และการ Extend Layout
[Laravel - Blade Template] How to Create and Extend Layout

การสร้าง Layout

การสร้าง Layout เป็นการสร้างเว็บเพจด้วย HTML เช่นเดียวกับหน้าเว็บทั่วไป แต่แทนค่าส่วนต่างๆที่จะใช้โดย child view ด้วย directives ซึ่งมี directives ที่เกี่ยวกับ Layout ดังนี้
  • @section ใช้ประกาศ section ในส่วนของเนื้อหา
  • @show ใช้ประกาศ section และใช้แสดงเนื้อหาของ section นั้น
  • @yield ใช้แสดงเนื้อหาของ section ที่ระบุ
ตัวอย่าง เช่น
@section('sidebar')
    This is the master sidebar.
@show

<div class="container">
    @yield('content')
</div>

Wednesday, December 11, 2019

[Laravel - Blade Template] การนำเข้า Template อื่นพร้อมกับกำหนด Parameter สำหรับ Template นั้น
[Laravel - Blade Template] How to Include Other Template with Parameters

โดยปกติแล้ว เวลาเราต้องการเพิ่ม template อื่นใน template หลักของเรา เราจะใช้โค้ดดังนี้
@include('menu.index')
Template ดังกล่าวถูกทำขึ้น เพื่อนำเข้าไปใช้ประโยชน์ในหลาย template หลัก เช่น template ของส่วนเมนูที่ต้องนำไปเพิ่มเข้าทุก template ที่ต้องการแสดงส่วนของเมนู

แต่ในบางครั้ง เราต้องการส่ง parameter เข้าไปยัง template ดังกล่าวด้วย เช่น การกำหนดว่า template หลักที่เพิ่ม template นั้นๆเข้ามาเป็น template เกี่ยวกับอะไร เพื่อแสดงเมนูในส่วนที่เกี่ยวข้องแตกต่างจากส่วนอื่น เป็นต้น เราสามารถใช้โค้ดดังนี้
@include('menu.index', ['group'=> 'users'])
จากตัวอย่าง เราส่ง parameter ชื่อ group เข้าไปยัง template menu.index ทำให้ใน template ดังกล่าว เราสามารถใช้ตัวแปร $group ได้