###### laravel
# 斬卍 Laravel 8 卐佛
# [開始之前,先裝個composer♪ 吧](https://laravel.com/docs/8.x#installation-via-composer)
composer create-project laravel/laravel:^8.0 {專案名稱}
# [阿凱激推!輕鬆輸出成EXCEL!](https://docs.laravel-excel.com/3.1/getting-started/installation.html)
# 高速 ★體驗★ MVC建立
:::info
/.env => server環境設定
先做
* C=> 在Controllers創建carController.php
```php=
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class carController extends Controller
{
public function show(){
return view('car');
}
}
```
* V=>
因為在function show()裏頭提到view('car');
所以去resourses\views創建car.blade.php頁面(當一般html寫就行)
* M=>
抱歉,這次沒有M
因為沒有用到資料所以沒有M的部分
* Routes=>
去routes\web.php說「聽好了Laravel 8,car.blade.php怎麼跟得上carController.php」
web.php:「carController.php,我叫你去show(),你給我show()」
```php=
//一般使用習慣是use 擺最上面
use App\Http\Controllers\carController;
// 輸入網址 localhost/car
Route::get('/car', [ carController::class,'show']);
```
最後下localhost/car顯示car.blade.php頁面
:::
## 使用terminal指令 ♪建立controller吧~♪
:::success
建立{controllerName}控制器元件內容空白
`php artisan make:controller {controllerName}`
建立{controllerName}控制器元件內含function __invoke
`php artisan make:controller {controllerName} --invokable`
建立{controllerName}控制器元件內含CRUD
`php artisan make:controller {controllerName} --resource`
列出目前所有的路由URL
`php artisan route:list`
* controller的function區塊對應的Action

:::
## 把DB透過controller ♪串 ♫起 ♪來
:::warning
在laravel資料夾專案路徑內開啟git bash
建立名字叫做xxxController的控制器元件內含CRUD(--resource)
`php artisan make:controller xxxController --resource`
先去routes\web.php嗆它
```php
use App\Http\Controllers\xxxController;
Route::resource('/xxx', xxxController::class);
```
在views下再建立xxx資料夾下再建立index.blade.php
index.blade.php建立一個連到{{route('xxx.create')}}的a tag
回到 xxxController.php去告訴它「我的index是xxx的形狀唷」
```php
public function index(){
//傳回 view\xxx\index.blade.php的頁面
return view('xxx.index');
}
public function create(){
//顯示字串
echo "這裡是xxx 的CREATE頁面>_0";
}
```
:::
:::info
先在專案目錄內的.env檔檢查是否與要使用的DB吻合
```clike
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
```
在terminal輸入指令自動生成5個table
`php artisan migrate`
在自訂的xxxController.php內呼叫DB並使用原生SQL語法
```php
///code位置自行微調
use Illuminate\Support\Facades\DB;
$data = DB::select('select * from users');
```
可以在index.blade.php(假設上面是放在index function的狀況)撈資料
```php=
//假設 users 有id, name, email欄位且有資料
//假設route使用 $data 傳入
<table>
<tr>
<td>id</td>
<td>name</td>
<td>email</td>
<tr>
// 開始使用foreach的起始指令
@foreach( $data as $key => $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->email}}</td>
<tr>
// 結束foreach的指令
@endforeach
```
:::
## Model~♪ 模擬戰
:::success
1. migration是對table結構異動的紀錄
2. 透過model可以控制欄位結構或關聯其他table,
3. 再透過controller撈資料,
4. view顯示資料。
在terminal輸入指令後會在/app/Models/建立xxx.php
`php artisan make:model xxx -m`
如果加上 -m 就會...同時建立migration ★よ
同時在/database/migration/建立 yyyy_mm_dd_######_create_xxx_table.php
如果只想單獨建立migration
`php artisan make:migration create_xxx_table`
* 設定欄位名稱
```php
public function up()
{
Schema::create('xxx', function (Blueprint $table) {
$table->id();
// 資料型態 string ,欄位名name
$table->string('name');
// 資料型態 string ,欄位名cc
$table->string('cc');
$table->timestamps();
});
}
```
打開terminal生成table
`php artisan migrate`
==:anger:在有資料的狀況下使用rollback會導致資料消失,資料請先備分:anger:==
如果你==執行migrate後==不幸做錯,想要回復上一動...
`php artisan migrate:rollback`
如果你真的很他媽衰小★よ,想要回復n動...
`php artisan migrate:rollback --step=n`
* 設定Models/xxx.php
```php
class xxx extends Model
{
use HasFactory;
protected $table='xxx';
protected $fillable=[
'欄位1',
'欄位2',
'欄位3',
];
}
```
在xxxController.php裡呼叫model
```php
use App\Models\xxx;
public function index()
{
// $data=DB::select('select * from xxx');
$data = xxx ::all();
return view('xxx.index',['data'=>$data]);
}
```
:::
## 想把 好多♪ 好多♫ table♪ 串在一起
:::warning
* 我有個 aaa桑☆ 跟 xxx君★ 在一起,他們有個共同興趣 LOL
`php artisan make:model aaa -m`
```php=
// model\aaa 的class內加上
protected $fillbase=[
'LOL_id',
'name',
];
public function xxx()
{
return $this->belongsTo(xxx::class);
}
// model\xxx 的class內加上
public function aaa(){
return $this->hasOne(aaa::class);
}
```
:::