```php Route::pattern('id', '[0-9]+'); Route::get('/', 'ProductController@index')->name('index'); Route::get('/product', 'ProductController@index')->name('product.index'); Route::get('/product/{id}', 'ProductController@show')->name('product.show'); Route::post('/cart/store', 'CartController@store')->name('cart.store'); Route::get('/cart', 'CartController@index')->name('cart.index'); Auth::routes(); Route::get('/home', 'ProductController@index')->name('home'); ``` ``` 2. 修改控制器`\專案\app\Http\Controllers\CartController.php` ```php public function index(Request $request) { $carts = $request->user()->carts()->get(); return view('cart.index', compact('carts')); } ``` - 目前登入使用者是`$request->user()`,因為我們有設模型關聯,所以,可以輕鬆抓出目前登入者的所有購物車資料,即$`request->user()->carts()`最後利用`get()`去抓取有資料出來即可。 - 記得要在index()中加入`Request $request`,因為我們要利用`$request->user()`來抓取目前登入者的資訊。 3. 接著建立新的視圖模板以顯示所有購物車內,先在`\專案\resources\views\`底下建立`cart`目錄,然後建立`\專案\resources\views\cart\index.blade.php`,我們先做個形狀出來即可,真正的功能後續再加入: ``` ```markup @extends('layouts.app') @section('content') 我的購物車
```markup @extends('layouts.app') @section('content') 我的購物車
```markup
```javascript swal('加入購物車成功', '', 'success') .then(function() { location.href = '{{ route('cart.index') }}'; }); ``` ``` [![到GitHub觀看此單元程式異動](https://campus-xoops.tn.edu.tw/uploads/tad_book3/image/34/to_github.png)](https://github.com/tad0616/myshop/commit/a4eaae72e4a02b0db46870ad6913557e6fe9ae33)
進階搜尋