---
# System prepended metadata

title: Laravel常見錯誤處理&自定義技巧

---

# Laravel常見錯誤處理&自定義技巧
常見錯誤
出現Syntax error or access violation:1071 Specified key was too long; max key length is 767 bytes的錯誤
解決方法：

開啟 app\Providers\AppServiceProvider.php

加入以下程式碼：

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
出現Argument 1 passed to Illuminate\Config\Repository::__construct() must be of the type array, null given, called in /Users/javck/Sites/laravel8/vendor/larapack/hooks/src/Composer.php on line 84
解決方法：
問題是composer.json語法錯誤

使用composer出現以下錯誤
[InvalidArgumentException]
Script “post-install-cmd” is not defined in this package

解決方法：
請開啟Terminal候輸入 composer global update即可

自定義技巧
限制Laravel內建分頁功能的連結數量
Step 1.發布 vendor 範本

php artisan vendor:publish --tag=laravel-pagination

Step 2.編輯bootstrap-4.blade.php，如下：

resources/views/vendor/pagination/bootstrap-4.blade.php

@if ($paginator->hasPages())
<ul class="pagination" role="navigation">
    {{-- Previous Page Link --}}
    @if ($paginator->onFirstPage())
        <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
            <span class="page-link" aria-hidden="true">&lsaquo;</span>
        </li>
    @else
        <li class="page-item">
            <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a>
        </li>
    @endif

    <?php
        $start = $paginator->currentPage() - 2; // show 3 pagination links before current
        $end = $paginator->currentPage() + 2; // show 3 pagination links after current
        if($start < 1) {
            $start = 1; // reset start to 1
            $end += 1;
        } 
        if($end >= $paginator->lastPage() ) $end = $paginator->lastPage(); // reset end to last page
    ?>

    @if($start > 1)
        <li class="page-item">
            <a class="page-link" href="{{ $paginator->url(1) }}">{{1}}</a>
        </li>
        @if($paginator->currentPage() != 4)
            {{-- "Three Dots" Separator --}}
            <li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
        @endif
    @endif
        @for ($i = $start; $i <= $end; $i++)
            <li class="page-item {{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
                <a class="page-link" href="{{ $paginator->url($i) }}">{{$i}}</a>
            </li>
        @endfor
    @if($end < $paginator->lastPage())
        @if($paginator->currentPage() + 3 != $paginator->lastPage())
            {{-- "Three Dots" Separator --}}
            <li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
        @endif
        <li class="page-item">
            <a class="page-link" href="{{ $paginator->url($paginator->lastPage()) }}">{{$paginator->lastPage()}}</a>
        </li>
    @endif

    {{-- Next Page Link --}}
    @if ($paginator->hasMorePages())
        <li class="page-item">
            <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">&rsaquo;</a>
        </li>
    @else
        <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
            <span class="page-link" aria-hidden="true">&rsaquo;</span>
        </li>
    @endif
</ul>
@endif
自定義分頁
參考網址

發表於  HackMD
 111
讚賞 收藏 訂閱