Category Archives: Laravel

Laravel Pagination

Eloquent Example

$honor = AchievementM::wherenotNull('id')
                ->orderBy('sort_order', 'asc')
                ->paginate(1);

HTML

@php
	$class = '';
	if($honor->onFirstPage() == true){
		$class = '-disable';
	}
@endphp
<a href="{{ $honor->previousPageUrl() }}" class="btn -btn-lg {{ $class }}">上一頁</a>

@php
	$class = '';
	if($honor->hasMorePages() == false){
		$class = '-disable';
	}
@endphp
<a href="{{ $honor->nextPageUrl() }}" class="btn -btn-lg {{ $class }}">下一頁</a>

Common Functions

lastPage();
//Number of last page
count()
; //Item count of this page
total()
; //Item count of all items
previousPageUrl()
nextPageUrl()
onFirstPage()
hasMorePages()
hasPages()
perPage()
 //Page size
items()

Reference:

Where with Subquery

Laravel Example

SQL

 

Reference:

  1. https://justcode.ikeepstudying.com/2016/08/mysql%E5%85%A5%E9%97%A8-%E4%B9%9D-%E5%AD%90%E6%9F%A5%E8%AF%A2-subquery/
  2. https://ithelp.ithome.com.tw/articles/10032121

Order by Array

Sample

Reference

  1. https://stackoverflow.com/questions/8055138/mysql-sort-order-by-array-value
  2. https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_field
  3. Could be replaced with orderRaw()
    https://laravel.com/docs/8.x/queries

Get Daily Data using Laravel Query Builder

Query Builder

 

Laravel API

Token

Reference:

  1. https://laravel.com/docs/5.8/api-authentication