【问题标题】:How do i Insert information to Order-OrderProduct tables MySQL Laravel我如何将信息插入到 Order-OrderProduct 表 MySQL Laravel
【发布时间】:2018-09-05 02:03:36
【问题描述】:

我正在使用 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 会话来保存购物车数据。我还有一个用于将订单存储到数据库的订单控制器。 问题是我如何正确插入 order 和 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-用户有很多订单,订单属于一个用户。 2-订单有很多产品。产品属于许多订单。 3-用户通过订单有很多产品。您可以使用属于模型的创建方法添加,以便创建新订单,然后使用创建方法来创建产品。看文档很清楚laravel.com/docs/5.6/…
  • 您可以使用关系添加相关项目。关系上的create() 方法接受多个实例。

标签: php mysql laravel


【解决方案1】:

将它们包装在事务中并照常插入:

DB::transaction(function() use($request) {

    $oldCart = Session::get('cart'); 
    $cart = new Cart($oldCart);

    $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();

    $orderProducts = [];
    foreach ($cart->items as $productId => $item) {
        $orderProducts[] = [
            'order_id' => $order->id,
            'product_id' => $productId
            'quantity' => $item['qty']
        ];
    }
    OrderProduct::insert($orderProducts);

});

【讨论】:

    猜你喜欢
    • 2016-12-23
    • 2013-12-30
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多