變成carts跟cart_items兩個表格
一對多的程式
復原也要修改
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCartsAndCartItems extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('carts', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
Schema::create('cart_items', function (Blueprint $table) {
$table->id();
$table->foreignId('cart_id');
$table->foreignId('product_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('carts');
Schema::dropIfExists('cart_items');
}
}
Terminal下指令去更新資料庫php artisan migrate
改成連$data = DB::table('products')->get();
在資料表內有2個測試資料
在terminal執行php artisan serve
用POSTMAN可以看到localhost:8000/products
要新增 購物車的controller在terminal下指令
php artisan make:controller CarController --resource
自動產生的程式碼:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CarController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
修改內容:
use Illuminate\Support\Facades\DB;
跟
public function index()
{
$cart = DB::table('carts')->get()->first();
if(empty($cart)){
DB::table('carts')->insert(['created_at' =>now(),'updated_at' =>now()]);
$cart = DB::table('carts')->get()->first();
}
return response(collect($cart));
}
```
要加入 路由: Route::resource('carts', 'CartController');

再到terminal讓他跑起來php artisan serve

資料表內有建的資料

用POSTMAN查看localhost:8000/carts


cart_items裡面有一些資料

修改 程式碼帶入cart_items資料表

程式碼
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CartController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$cart = DB::table('carts')->get()->first();
if (empty($cart)) {
DB::table('carts')->insert([
'created_at' => now(),
'updated_at' => now()
]);
$cart = DB::table('carts')->get()->first();
}
$cartItems = DB::table('cart_items')
->where('cart_id', $cart->id)
->get();
$cart = collect($cart);
$cart['items'] = collect($cartItems);
return response(collect($cart));
}
}

-------------
用POSTMAN測試localhost:8000/carts

設定系統時間: 'timezone' => 'UTC',修改

改成'timezone' => 'Asia/Taipei',

測試確實有改過去

把cart_id改成2
再用POSTMAN測試顯示
這裡不知道為什麼圖片不能正常顯示~大家要看就把網址貼上就可以看到哦
大家明天見~