forked from summerblue/laravel-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\AddCartRequest; | ||
use App\Models\CartItem; | ||
use Illuminate\Http\Request; | ||
|
||
class CartController extends Controller | ||
{ | ||
public function add(AddCartRequest $request) | ||
{ | ||
$user = $request->user(); | ||
$skuId = $request->input('sku_id'); | ||
$amount = $request->input('amount'); | ||
|
||
// 从数据库中查询该商品是否已经在购物车中 | ||
if ($cart = $user->cartItems()->where('product_sku_id', $skuId)->first()) { | ||
|
||
// 如果存在则直接叠加商品数量 | ||
$cart->update([ | ||
'amount' => $cart->amount + $amount, | ||
]); | ||
} else { | ||
|
||
// 否则创建一个新的购物车记录 | ||
$cart = new CartItem(['amount' => $amount]); | ||
$cart->user()->associate($user); | ||
$cart->productSku()->associate($skuId); | ||
$cart->save(); | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Models\ProductSku; | ||
|
||
class AddCartRequest extends Request | ||
{ | ||
public function rules() | ||
{ | ||
return [ | ||
'sku_id' => [ | ||
'required', | ||
function ($attribute, $value, $fail) { | ||
if (!$sku = ProductSku::find($value)) { | ||
return $fail('该商品不存在'); | ||
} | ||
if (!$sku->product->on_sale) { | ||
return $fail('该商品未上架'); | ||
} | ||
if ($sku->stock === 0) { | ||
return $fail('该商品已售完'); | ||
} | ||
if ($this->input('amount') > 0 && $sku->stock < $this->input('amount')) { | ||
return $fail('该商品库存不足'); | ||
} | ||
}, | ||
], | ||
'amount' => ['required', 'integer', 'min:1'], | ||
]; | ||
} | ||
|
||
public function attributes() | ||
{ | ||
return [ | ||
'amount' => '商品数量' | ||
]; | ||
} | ||
|
||
public function messages() | ||
{ | ||
return [ | ||
'sku_id.required' => '请选择商品' | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class CartItem extends Model | ||
{ | ||
protected $fillable = ['amount']; | ||
public $timestamps = false; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function productSku() | ||
{ | ||
return $this->belongsTo(ProductSku::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
database/migrations/2018_12_22_071404_create_cart_items_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateCartItemsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('cart_items', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('user_id'); | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
$table->unsignedInteger('product_sku_id'); | ||
$table->foreign('product_sku_id')->references('id')->on('product_skus')->onDelete('cascade'); | ||
$table->unsignedInteger('amount'); | ||
}); | ||
} | ||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('cart_items'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters