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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)