- 有問題歡迎到 Laravel.tw 台灣社群 發問
- 我寫的超細,請耐心照步驟一步步做,寫這些遠比你實作起來更多更累啊…… 我真應該錄影片就好的
- 如果還是有看不懂的一定要問,碰到太多新手不敢問的,都新手了還不問只好永遠當新手
- 有
$開頭的表示要下的指令
- 下載安裝 Composer 按此,如果你是 Mac/Linux 環境,就是下這些指令,一個
$符號表示一行指令:
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"- 用
$ php composer.phar -v或$ composer -v來確認你安裝成功了 - 用
$ php composer.phar裝成功的朋友,請自動將後續指令裡的composer置換成php composer.phar - 建立 Laravel 專案
$ composer create-project laravel/laravel L53_CRUD會下載 Laravel 原始碼到 L53_CRUD 專案資料夾 $ cd L53_CRUD進入專案資料夾,沒特別說明的話後續指令都是在這資料夾下執行。- (optional) 如果你是用
$ php composer.phar跑 composer 的人,記得把composer.phar檔複製一份到專案資料夾裡$ cp ../composer.phar ./ - 執行
$ php artisan,如果沒有錯誤訊息,就是 Laravel 裝好了 - 如果有問題,可能是 Laravel 相依的 PHP 套件還沒裝好,下
$ composer install來安裝 - 執行
$ php artisan serve並打開 http://localhost:8000/ 應該可以看到 Laravel 專案初始的首頁 - 按
<command> + c(Windows:<ctrl> + c) 可以終止artisan serve產生的 Web Server
- 請練習看 commits 做了什麼事,有問題歡迎拿 commit 的 url 到 Laravel.tw 台灣社群 發問
- 利用
php artisan serve啟動 Web Server,並開啟 Laravel 首頁 http://localhost:8000/ - 在
routes/web.php裡增加 routeGET /contactUs,並指向ContactUsController@index - 打開 http://localhost:8000/contactUs 認識一下找不到
ContactUsController@index時的錯誤訊息 - 下指令建 controller
$ php artisan make:controller ContactUsController - 增加 index method 在 controller 裡,並
return 'This is Contact Us Page.'; - 測試 route 和 controller,重新整理 http://localhost:8000/contactUs 應該可以看到
This is Contact Us Page. - 修改 index method 回傳
return view('contact.index');對應的檔案是resources/views/contact/index.blade.php - 重新整理 http://localhost:8000/contactUs 認識一下找不到
resources/views/contact/index.blade.php時的錯誤訊息 - 手動建立
resources/views/contact/index.blade.php檔,內容放This is conatct us view file.就好 - 重新整理 http://localhost:8000/contactUs 應該會看到
This is conatct us view file. - 在
contact/index.blade.php裡寫一般的 HTML,但 CSS 要放在public/css下,JS 要放在public/js下,引用時開頭要加/ - 舉例,要引用
jquery.min.js請下載後放在public/js下;要引用bootstrap.min.css請下載後放在public/css下,在contact/index.blade.php裡這樣寫:
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<script src="/js/jquery.min.js"></script>- 重新整理 http://localhost:8000/contactUs 看到「聯絡我們」表單 UI
- 聯絡我們表單舉例,重點在
action="/submitContact"和method='POST'
<form action="/submitContact" method="POST">
// 中間略
<button type="submit">送出</button>
</form>
- 到 http://localhost:8000/contactUs 輸入假資料按
送出試試,認識一下找不到POST /submitContact時的錯誤訊息 - 在 route 中加入
POST /submitContact並指向ContactUsController@store - 在
ContactUsController中新增storemethod 來處理 POST 進來的資料 - 在
storemethod 中寫dd( request()->all() );可以 die & dump 所有 POST 進來的資料,用做 debug - 再回到 http://localhost:8000/contactUs 輸入假資料按
送出,認識一下TokenMismatchException的錯誤訊息 - 在表單加入
{{ csrf_field() }},如下:
<form action="/submitContact" method="POST">
{{ csrf_field() }}
// 中間略
<button type="submit">送出</button>
</form>
- 再回到 http://localhost:8000/contactUs 輸入假資料按
送出,畫面上應該會出現你剛剛輸入的資料了。
- 建立
l53_crud資料庫 - 建立
homestead使用者,設密碼為secret - (重要) 把
l53_crud資料庫的權限開給homestead使用者,先權限全開吧 - 編輯
L53_CRUD/.env檔
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=l53_crud
DB_USERNAME=homestead
DB_PASSWORD=secret- 記得如果你是用
$ php artisan serve開發,請關掉重啟才會讀取到新的.env設定 - 建議先用一般的 MySQL client (ex: Sequel Pro) 程式連連看資料庫,連得上的話,照著設定一定也會可以連得上。
- 執行
php artisan migrate如果沒有錯誤訊息就表示 Laravel 連線資料庫正常,並用 MySQL client 檢查資料庫(i.e. database, 或簡稱 DB) 中多了哪些 tables - 用 Mac 的人可以裝 MAMP,但預設的 Port 是 8889,用 Open WebStart Page 可以看到資料庫相關設定
- 用 Windows 的人可以裝 XAMPP / WAMP 等,或是裝 Wagon 一次搞定開發環境,請參考這裡
-
剛剛你在 MySQL client 裡應該會看到 users、password_reset、migrations 三個 tables,其中 users 和 password_reset 表是透過這些 migration 檔來建立的:
database/migrations/2014_10_12_000000_create_users_table.phpdatabase/migrations/2014_10_12_100000_create_password_resets_table.php
-
現在,我們要建我們自己的 migration 檔,透過 migration 檔在 DB 中建 table
$ php artisan make:model Contact -m會做兩件事- 產生新的 migration 檔在
database/migrations/2016_09_XX_XXXXXX_create_contacts_table.php - 產生新的 model 檔在
app/Contact.php
- 產生新的 migration 檔在
- 編輯
2016_09_XX_XXXXXX_create_contacts_table.php檔,檔名打 X 的部份是會隨時間改變的,可忽略,把「聯絡我們」表單中要存的欄位建立起來:
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->string('firstName');
$table->string('lastName');
$table->string('email');
$table->string('phone')->nullable();
$table->string('message');
$table->timestamps();
});- 再執行
$ php artisan migrate,然後再用 MySQL client 檢查資料庫,你的contactstable 應該就建好了 - (資料表所有資料會不見唷!!!) 要清掉重建所有 tables 可以用
$ php artisan migrate:refresh - 只是清掉不重建
$ php aritsan migrate:reset
- Live Demo Only:
Tinker的用法,並示範利用 Model 存取資料表資料 So Easy~
- 注意 namespace,記得是用
\App\Contact來使用 Model,不然就需要用use App\Contact; - 編輯
ContactUsController@store將 POST 進來的資料存進資料庫
public function store()
{
var_dump(request()->all());
$newContact = new \App\Contact();
$newContact->firstName = request()->get('name');
$newContact->lastName = request()->get('surname');
$newContact->email = request()->get('email');
$newContact->phone = request()->has('phone') ? request()->get('email') : '';
$newContact->message = request()->get('message');
$newContact->save();
}- Live Demo Only: 介紹
MassAssignmentException和Contact::create([...]);及$fillable屬性
$ php artisan make:auth這樣就建好了! 就這麼簡單~$ php artisan make:controller AdminContactController --resource建立 AdminContactController- 製作 Contact 列表頁面,定義 route
/admin/contacts並使用AdminContactController@index顯示出來 - 製作 Edit Contact 頁,定義 route
/admin/contacts/edit/{id}並使用AdminContactController@edit顯示出來 - 定義 PUT request 到
/admin/contacts/{id}時,觸發AdminContactController@update,來更新 contacts 資料。 - 定義 DELETE request 到
/admin/contacts/{id}時,觸發AdminContactController@destroy,來刪除 contacts 資料。
- 運用 middleware 來過濾是否為 Admin,只有 Admin 才可以進入
/admin/contacts