dpfl37651 2017-07-22 06:09
浏览 68
已采纳

在laravel中保存数据库中的联系表格信息

I'm working with laravel 5.4.

I try to make sell part (store) in my website and for items that I need to sell I don't want to use regular way!

what that means? usually in commerce websites when you set to order a product you will go to some place named cart and there has all calculations, you pay and it's done.

for me I try to get info with contact form, each item has a form inside and user fulfill the form then the data of user and product will be sent by email to admin so far everything is working but I also want to save these data that been sent by email in other table as Order_table and show it to user in their panel that they've been requested to buy those item and if the items been paid or not yet.

Question: How to save these data in table include sum the price base on selected quantity by user?

this is my form that is in my items page:

<form class="form-horizontal" action="{{route('ask_product')}}" method="POST"  id="contact_form">
            {{ csrf_field() }}

            <div class="form-group">
              <label class="col-md-3 control-label">UserName</label>
              <div class="col-md-9 inputGroupContainer">
                <div class="input-group">
                  <span class="input-group-addon"><i class="fa fa-user"></i></span>
                  <input  name="username" value="{{ Auth::user()->username }}" class="form-control"  type="text" readonly>
                </div>
              </div>
            </div>

            <!-- Text input-->
            <div class="form-group">
              <label class="col-md-3 control-label">E-Mail</label>
                <div class="col-md-9 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                    <input name="email" value="{{ Auth::user()->email }}" class="form-control"  type="text" readonly>
                </div>
              </div>
            </div>

            <div class="form-group">
              <label class="col-md-3 control-label">Product</label>
              <div class="col-md-9 inputGroupContainer">
                <div class="input-group">
                  <span class="input-group-addon"><i class="fa fa-product-hunt"></i></span>
                  <input  name="product" value="{{ $product->title }}" class="form-control"  type="text" readonly>
                </div>
              </div>
            </div>

            <div class="form-group">
              <label class="col-md-3 control-label" for="type">Quantity</label>
              <div class="col-md-9 inputGroupContainer">
                <div class="input-group">
                  <span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
                  <select class="form-control" id="type" name="quantity">
                    <option value="">Select Quantity</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="More than 5">More than 5</option>
                  </select>
                </div>
              </div>
            </div>

            <div class="form-group">
              <label class="col-md-3 control-label">Note to seller</label>
              <div class="col-md-9 inputGroupContainer">
                <div class="input-group">
                  <span class="input-group-addon"><i class="fa fa-commenting-o"></i></span>
                  <textarea name="note" id="text" placeholder="Your note to seller here..." class="form-control" rows="8" ></textarea>
                </div>
                <h6 class="pull-right" id="count_message"></h6>
              </div>
            </div>

            <div class="text-center">
              <input type="submit" class="btn btn-block btn-success" value="Send">
            </div>
        </form>

this is the form validator and email sender:

public function ask() {
      return view('frontend.shopsingle');
    }

    public function postask(Request $request) {
      $this->validate($request, array(
        'username' => 'required',
        'email' => 'required|email',
        'product' => 'required',
        'note' => 'required|min:10|max:500',
        'quantity' => 'required',
      ));

      $data = array(
        'username' => $request->username,
        'email' => $request->email,
        'product' => $request->product,
        'note' => $request->note,
        'quantity' => $request->quantity,
      );

      Mail::send('emails.ask', $data, function($message) use ($data) {
        $message->from($data['email']);
        $message->to('robertnicjoo@outlook.com');
        $message->subject($data['product']);
      });

      Session::flash('flash_message', 'Your Order was sent. Our sell team will contact you shortly.');
      return redirect()->back();

    }

here are my routes for that form:

    Route::get('/ask', 'ContactController@ask')->name('ask_product');  

Route::post('/ask', 'ContactController@postask');
  • 写回答

3条回答 默认 最新

  • douyin2962 2017-07-22 06:21
    关注

    after validation you can write the code to store the data into table

    DB::table('Order_table')->insert(
        ['username' => $request->username,
        'email' => $request->email,
        'product' => $request->product,
        'note' => $request->note,
        'quantity' => $request->quantity]
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图