PHP新手30天實戰金流
, Laravel6
由於架構的概念建立沒辦法短時間學成,加上有許多前端畫面需要顯示,因此放棄全部自己 DIY。 沙拉在 github 上找到一個 簡易商城的專案,實作部分:
* 用戶登入
* 商品 (多維 SKU)
* 購物車
* 結帳
* 評分
* 付款(沒有實作,直接跳過)
* 退款(沒有實作,直接跳過)
* (未加入優惠卷)
* 後台管理
因此決定,在這個 project 上完成金流部分。
真是不好意思,可能讓大家失望了><
git clone https://github.com/ycs77/Laravel-shop.git
後,進到 laravel-shop 資料夾。composer install
: 自動將所有相依套件安裝在 vendor 資料夾php artisan key:generate
。若無 key,會產生 production.ERROR: No application encryption key has been specified.
( APP_KEY 會設定在 .env 裡。)CREATE DATABASE laravel_shop;
npm install
, npm run dev
。 若沒有 node_modules 應該會出現錯誤: Mix manifest does not exist
php artisan serve
成功開啟Register
遇到問題 : Swift_TransportException Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required " http://localhost:8000/register
解決:
php artisan config:clear
註冊帳號:
用測試信箱收驗證信(可參考 [day4] 教學):
php artisan db:seed
ErrorException : Trying to get property 'address' of non-object
at /Users/sarahcheng/Documents/php/taiwan/Laravel-shop/database/factories/OrderFactory.php:25
21| $ship = $faker->randomElement(array_keys(__('order.ship')));
22|
23| return [
24| 'address' => [
> 25| 'address' => $address->full_address,
UserAddress model 沒有 full_address 欄位
public function addresses()
{
return $this->hasMany(UserAddress::class);
}
protected $fillable = [
'city',
'district',
'address',
'zip_code',
'contact_name',
'contact_phone',
'last_used_at',
];
public function getFullAddressAttribute()
{
return "{$this->zip_code} {$this->city}{$this->district}{$this->address}";
}
改成
23| return [ 24| 'address' => [ > 25| 'address' => $address->getFullAddressAttribute(),
轉址到 填寫地址 http://localhost:8000/user_addresses
出現 error:
Facade\Ignition\Exceptions\ViewException
Undefined variable: addresses (View: /Users/sarahcheng/Documents/php/taiwan/Laravel-shop/resources/views/user_addresses/index.blade.php)
public function index(Request $request)
{
$user_address = $request->user()->addresses;
return view('user_addresses.index', compact('user_address'));
}
views/user_addresses/index.blade.php
看問題發生點:
@foreach ($addresses as $address)
@foreach ($user_address as $address)
填寫地址後,按送出。結果 error :
Facade\Ignition\Exceptions\ViewException
Route [user_addresses.create] not defined. (View: /Users/sarahcheng/Documents/php/taiwan/Laravel-shop/resources/views/user_addresses/index.blade.php)
解決: 將 route 命名
->name('user_addresses.create');
成功新增、刪除和修改地址
改良完整地址產生方法:
<option value="{{ $address->id }}">{{ $address->full_address }} {{ $address->contact_name }} {{ $address->contact_phone }}</option>
<option value="{{ $address->id }}">{{ $address->getFullAddressAttribute() }}</option>
拿掉自動結束訂單的功能
如果沒拿掉會出錯:
Predis\Connection\ConnectionException: Connection refused [tcp://127.0.0.1:6379] in /Users/sarahcheng/Documents/IT/php/Laravel-shop/vendor/predis/predis/src/Connection/AbstractConnection.php:155
晚生學習分享所學經驗,若內容有誤或不清楚,煩請不吝指教!更是歡迎各位大神多多補充,感謝萬分!