# laravel 套件(flash .購物車 多國語系 後台) Excel
## repository之類的必用
https://github.com/Mombuyish/Laravel-Oh-Generators
## FCM 雲端丟訊息
https://github.com/brozot/Laravel-FCM
## 用戶交換私人消息,例如內部“收件箱”系統
## debugger
必裝不多說
如何關掉

config的這個設定黨
在env 設true 跟 false
false就會關掉
## teleScope
https://laracasts.com/series/laravel-from-scratch-2018/episodes/28
可以用看看
## 圖片
Laravel-medialibrary
他會幫你用rwd的圖片
還有一個最大特色
是網速不夠的情況
會幫他模糊
如果你存的不是defalut collection有名稱(regisn那個 model註冊)
你getMedia就要給你要取的
你model跑回圈如果沒有在->getUrl 會爆版
必看
https://www.youtube.com/watch?v=ex0o2eRhtp4&list=PLe30vg_FG4OQ47ovsjgSSX5BZy_kqbuTO&index=15
介紹Pro
https://www.youtube.com/watch?v=oqW6vlJgXYE
官方文件
https://spatie.be/videos/discovering-laravel-media-library/introducing-laravel-media-library
太長了 沒看
https://www.youtube.com/watch?v=3eyftAR5ilo
pro版 有更好的ui 還能自由選擇上船
他還有三個開箱能用的 vue blade react
記住 blade的要搭配livewire 跟 alphe.js
使用
model定義 註冊方法 collection
```
public function registerMediaCollections(): void
{
$this->addMediaCollection(self::IMAGE_COLLECTION_NAME);
}
```
註冊
記得要加上trait
use InteractsWithMedia;
因為裡面有morph
取資料
getMdeia
```
@foreach ($customer
->getMedia(\Dolfan\Models\Constants\CustomerServiceConstantInterface::IMAGE_COLLECTION_NAME)
as $media )
<a href="{{ $media->getUrl() }}" data-lightbox="file" data-alt="問題">
<img src="{{ $media->getUrl() }}" alt="問題" style="width:200px; height:200px">
</a>
@endforeach
```
這樣指示能取資料跟關聯而已
controller放的話
```
public function uploadToTemp(UploadToTempRequest $request): ?MediaResource
{
$file = $request->file;
$key = $request->type_name;
$collectionName = data_get($request, 'collection_name');
if (is_null($collectionName)) {
$collectionName = 'default';
}
//model取資料是 因為要加入這個collection
$temp = $this->mediaTemp->where('key', $key)->first();
$media = $this->uploadMediaByModel($temp, $file, $collectionName);
if (!$media) {
return null;
}
return new MediaResource($media);
}
public function uploadMediaByModel(?HasMedia $model, UploadedFile $file, string $collectionName = 'default', ?string $customFileType = null): Media
{
if (is_null($model)) {
return null;
}
$fileName = Str::random(20);
$extension = $file->getClientOriginalExtension();
$customProperties = [FileConstantInterface::ORIGIN_FILE_NAME => $file->getClientOriginalName()];
if (filled($customFileType)) {
$customProperties[FileConstantInterface::MEDIA_CUSTOM_TYPE] = $customFileType;
$fileName = "{$customFileType}_{$fileName}";
}
return $model
->addMedia($file)
->usingFileName("{$fileName}.{$extension}")
->usingName($fileName)
->withCustomProperties($customProperties)
->toMediaCollection($collectionName);
}
```
service
```
public function uploadToTemp(UploadToTempRequest $request)
{
if ($request->hasfile('file')) {
$resource = $this->uploadService->uploadToTemp($request);
return $this->success($resource);
}
return $this->error();
}
```
## excel套件
基本的就不說了
安裝然後push vender的
建立exports
如何設立header
實現介面withHeadings
如何自訂的事件
實現withEvents介面
如何自動長寬
實現介面ShouldAutoSize
下面是第一行顏色的code
```
class UsersExport implements FromCollection, WithHeadings, WithEvents, ShouldAutoSize
{
/**
* @return \Illuminate\Support\Collection
*/
基本的 倒出就用這個
例如
return Excel::download(new UsersExport(), 'members.xlsx');
public function collection()
{
return OrderTab::all();
}
/**
* Write code on Method.
*
* @return response()
*/
//寫header的
public function headings(): array
{
return [
'id',
'uuid',
'name',
'type',
'created_at',
'updated_at',
];
}
/**
* Write code on Method.
*
* @return response()
*/
//自訂事件
public function registerEvents(): array
{
return [
//在afterSheet生命週期使用
AfterSheet::class => function (AfterSheet $event) {
// 设置背景色 這邊就使用到PhpSpreadsheet了 自訂style 第一行第一個到第幾個
$event->sheet->getDelegate()->getStyle('A1:F1')
->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()
->setARGB('fde9d9');
},
];
}
}
```
必看
https://www.itsolutionstuff.com/post/laravel-maatwebsite-set-background-color-of-column-exampleexample.html
https://segmentfault.com/a/1190000018375784
https://laraveldaily.com/laravel-excel-export-formatting-and-styling-cells/
## 圖片上傳的UI
https://www.youtube.com/watch?v=GRXaCfS1qj0
必用
## 圖片壓縮 media那個就能取代了 真的沒辦法再用這個 很舊的
https://github.com/Intervention/image
上面那個就有這個套件了
## 權限
Laravel-permission
https://www.youtube.com/watch?v=NgToi0uiMNQ
https://www.youtube.com/watch?v=nx6eGEKnkwU&list=PLe30vg_FG4OQ47ovsjgSSX5BZy_kqbuTO&index=9
跟
保鑣
這兩個都很活躍
**Spatie的優勢:**
更好的文檔(README 中沒有提到一些 Bouncer 的方法)
更容易理解的數據庫結構
syncRoles() 方法代替 delete-insert 方法
一些刀片命令 - @role 和 @hasanyrole
能夠使用多個守衛
**保鏢的優勢:**
創建角色/能力並分配它——一言以蔽之
根據模型或其實例允許或禁止權限
更好的緩存機制
更健壯的數據庫結構和一些更有用的字段
https://laravel-news.com/two-best-roles-permissions-packages
## excel
https://hackmd.io/@javck/ByJgF8HRP/%2FXz3Q2fLIS8mi8KQm3Fpmjw
## 後台
https://hackmd.io/@javck/ByJgF8HRP/%2FbQyTk4o7Rqm8WFzGowWjVw
## 多國語系
https://hackmd.io/@javck/ByJgF8HRP/%2FGTiGFf1JSYyrF9sBjhl78A
## flash套件
https://hackmd.io/@javck/ByJgF8HRP/%2F64nMsOpgQoCAGGbbcLSpXw
第三方套件 Flash Message 訊息顯示
看文章
## 購物車
https://hackmd.io/@javck/ByJgF8HRP/%2FFblVe7NHTDSDitg3NXjU-Q
## Laravue 後台套件
Laravel + VueJS = Laravue
## darryldecode shop
### 要注意的
購物車如果更新資料
他會把最新的放下面
所以要用sortBy(id)去排序
才不會亂掉
`這邊很重要`

## laravel PDF
### 安裝套件
https://github.com/barryvdh/laravel-dompdf
照步驟 安裝
import
引入
use Barryvdh\DomPDF\Facade as PDF;
### 中文亂碼
https://blog.yuyansoftware.com.tw/2017/02/laravel-pdf/
重點重點
上傳 load_font.php 和 ttf 到你 Laravel 的根目錄
不是把ttf放在 fonts下面 重點重點!!!!!!
## laravel 圖像驗證碼
大概樣子

正規的 https://github.com/mewebstudio/captcha
第一步安裝 composer require mews/captcha
第二步在cofing app那邊加上兩行
```
'providers' => [
// ...
Mews\Captcha\CaptchaServiceProvider::class,
]
```
```
'aliases' => [
// ...
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
```
第三步表單增加驗證碼
用下面兩個去選擇
可看這篇
[https://learnku.com/laravel/t/2895/extension-recommended-mewscaptcha-image-authentication-code-solution](https://learnku.com/laravel/t/2895/extension-recommended-mewscaptcha-image-authentication-code-solution)
captcha_img () - 返回 img 格式的验证码;
captcha_src () - 返回验证码的 url 地址。
第四步request要有回傳 看自己是放在login 還是註冊 去相關的controller加上驗證規則
找到驗證地方 加上 'captcha' => 'required|captcha'這句就好
這邊已經完成
### 進階
#### 換圖片
**js** <img src="{{captcha_src()}}" style="cursor: pointer" onclick="this.src='{{captcha_src()}}'+Math.random()">
參考這篇的
[https://learnku.com/articles/6736/laravel-code-captcha](https://learnku.com/articles/6736/laravel-code-captcha)
**ajax** 懶得用
可看這篇
[https://www.youtube.com/watch?v=EDZCx73vsm4](https://www.youtube.com/watch?v=EDZCx73vsm4)
#### 錯誤訊息
前提是你要有 中文版的語言包 沒的話一樣下面兩句加在英文那邊(改英文)
resources/zh-Tw/validation.php,在总数组中追加如下键值对:
`'captcha' => ':attribute 不正确。',`
在 attributes 数组中追加如下键值对:
`'captcha' => '验证码',`
## Cashier:處理付款
## Passport:提供 API 認證
去看auth那篇文章
## Socialite:協助處理 OAuth 認證
可以去看第三方登入文章
## Telescope:協助除錯的後台
https://www.youtube.com/watch?v=SR3RzIfeozI
主要給api用
## 錯誤追蹤
Laravel 中的錯誤跟踪:Bugsnag 與 Flare
https://www.youtube.com/watch?v=88UqUXhWwGA
## 新工具!Spatie Ray(2021)
splatie系列的喔
但他是付費的喔
###### tags: `Laravel`