我正在用laravel开发简单的电子商务网站用于学习目的 .
当客户下订单时,关于数据库关系和将数据插入order-order_product表的事情让我很困惑 .
用户迁移:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('address');
$table->string('phone');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
用户模型:
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
protected $attributes =[
'street' => 'no adress entered',
'city' => 'no city entered',
'phone' => 'no phone'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function orderproduct(){
return $this->hasMany('App\OrderProduct');
}
}
订单表:
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('customer_id')->unsigned();
$table->foreign('customer_id')->references('id')->on('users');
$table->string('name');
$table->string('address');
$table->string('phone');
$table->date('order_date');
$table->timestamps();
});
订单型号:
class Order extends Model
{
//Table Name
protected $table = 'orders';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamps =true;
public function user(){
return $this->belongsTo('App\User');
}
public function orderproduct(){
return $this->hasMany('App\OrderProduct');
}
}
产品表:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('img');
$table->string('name');
$table->string('desc');
$table->integer('quantity');//stokta kaç tane oldugu
$table->integer('price');
$table->timestamps();
});
产品型号:
class Product extends Model
{
//Table Name
protected $table = 'products';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamps =true;
public function orderproduct(){
return $this->belongsTo('App\OrderProduct');
}
}
order_product表:
Schema::create('order_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('order_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('quantity')->unsigned();
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders');
$table->foreign('product_id')->references('id')->on('products');
});
订单产品型号:
class OrderProduct extends Model
{
//Table Name
protected $table = 'order_product';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamps =true;
public function order(){
return $this->belongsTo('App\Order');
}
public function product(){
return $this->hasMany('App\Product');
}
}
我正在使用laravel会话来保存购物车数据 . 我也有一个ordercontroller用于存储订单到数据库 . 问题是如何正确插入订单和order_product表?首先我要插入订单然后到order_product表?例如,如果用户的购物车中有多个商品,因为order_product表中的product_id列需要是原子的,我需要插入多行 . 我可以从我的购物车访问product_id及其数量,但我无法设法正确循环并插入数据库 .
public function store(Request $request)
{
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
//dd(arrays_keys($cart->items)); // returns ids of products in cart
//dd($cart->items[1]['qty']); // returns quantity of item which has id 1
$order = new Order;
$order->name = $request->input('name');
$order->address = $request->input('address');
$order->phone = $request->input('phone');
$order->customer_id = auth()->user()->id;
$order->save();
$orderProduct = new OrderProduct;
//$orderProduct->product_id = ?? how to write in multiple rows if user has multiple items(so values will be atomic in product_id column)
//$orderProduct->quantity= ??
}
1 回答
将它们包装在事务中并像往常一样插入它们: