如何发送VueJS对象以存储在Laravel控制器中?

时间:2019-03-16 16:43:48

标签: javascript php database laravel vue.js

我有此方法,但是信息无法到达数据库,请帮帮我,我不知道为什么信息没有发送到控件。

当我发送正确完成的数据时,它们没有加载到数据库中,但是确实通过了控制器,因为它返回了前一条路线。可以在控制台中查看数据对象。

  

是控制器。控制器很好,因为我在另一部分中对其进行了调用,并且信息已正确添加。

public function store($data){
        $billing=new OSbilling();
        $billing->order_id=$data['order_id'];
        $billing->billing_number=$data['billing_number'];
        $billing->billed_timene=$data['billed_timene'];
        $billing->billed_travel=$data['billed_travel'];
        $billing->billed_hours=$data['billed_hours'];
        $billing->price=$data['price'];
        $billing->type=$data['type'];
        $billing->created_at=strtotime(Kusher::GetActualDate());
        if(isset($data['created_at'])){
            $billing->created_at=$data['created_at'];
        }
        if($billing->save()){
            return '200';
        }
        return '500';
}
  

当我发送正确完成的数据时,它们没有加载到数据库中,但是确实通过了控制器,因为它返回了前一条路线。可以在控制台中查看数据对象。

public function postStorefast($obj){

        $array=Input::all();

        $this->store($array);

        $order=OrderService::find(Input::get('order_id'));
        $billings=OSbilling::whereRaw('order_id='.$order->id)->get();
        $total_hours=0;
        foreach ($billings as $key => $billing) {
            $total_hours=$total_hours+$billing->billed_hours;;
        }
        $possible_hours=$order->execute_hours-$total_hours;

        if($possible_hours<=0 && $possible_hours >= 0 ){
            $OL=new OSorderLogsController();
            $log=array(
                "order_id"=>$order->id,
                "status_id"=>"6",
                "user_id"=>Auth::id(),
                "notes"=>"");
            $OL->postSave($log);
             $order->status = 6;
            $order->save();
        }

        return Redirect::back();
    }
  

这是HTML

<div class="row">
  <div class="col-xs-12">

    <div class="block">
      <div class="block-title">
        <h2>Creación de facturas</h2>
      </div>
      <div class="block-content">
      <div class="row">
        <div id="app">
          <div class="col-xs-12">

            <div class="form-group">
            <label class="col-md-2 text-right">Buscar orden (por OS u OSI)</label>
          <input type="text" v-model="term" @keyup.enter="search">
          <button v-on:click="search">Buscar</button>
            </div>
          </div>
          <div class="col-xs-12 table-responsive" v-if="ordersFound.length > 0">
          <hr>
          <small>
            En esta tabla puedes agregar las ordenes que se van a facturar
          </small>
            <table class="table table-bordered" >
              <thead>
                <tr>
                  <th>OSI</th>
                  <th>OS</th>
                  <th>Profesional</th>
                  <th>Actividad</th>
                  <th>Desplazamiento real</th>
                  <th>Valor ARL real</th>
                  <th>Valor ARL</th>
             <th>Horas/Unidades posibles</th> 
                  <th>Selección</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="of in ordersFound">
                  <td>{[{of.id}]} </td>
                  <td>{[{of.order_number}]} </td>
                  <td>{[{of.profesional}]} </td>
                  <td>{[{of.activity}]} </td>
                  <td>{[{of.travel_price}]} </td>
                  <td>{[{of.real_arl_price}]} </td>
                  <td>{[{of.arl_value_timene}]} </td>
            <td> {[{ of.possible_hours}]} </td> 


                  <td><button class="btn btn-success" @click="add(of)">Agregar</button></td>
                </tr>
              </tbody>
            </table>
          </div>


          <div class="col-xs-12 table-responsive" v-if="ordersSelected.length > 0">
          <hr>
          <h2>
            Ordenes a facturar
          </h2>
          <small>
            En esta tabla puedes modificar los valores de las ordenes
          </small>
          <div>
                        <div class="form-group">
              <label class="col-md-2 text-right"> Fecha factura</label>
              <input type="date" v-model="fechaFactura">
            </div>
            <div class="form-group">
              <label class="col-md-2 text-right"> Número factura</label>
              <input type="text" v-model="numeroFactura">
            </div>
          </div>
          <table class="table table-bordered">
            <thead>
                <tr>
                  <th>OSI</th>
                  <th>OS</th>
                  <th>Profesional</th>
                  <th>Actividad</th>
                  <th>Desplazamiento Real</th>
                  <th>Valor ARL real</th>
                  <th>Valor ARL tiempo no efectivo</th>
                  <th>Cantidad Desplazamiento</th>
                  <th>Número de horas <br>/ Unidades</th>
                  <th>Valor a facturar</th>

                  <th>Selección</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="(os, index) in ordersSelected">
                  <td>{[{os.id}]} </td>
                  <td>{[{os.order_number}]} </td>
                  <td>{[{os.profesional}]} </td>
                  <td>{[{os.activity}]} </td>
                  <td>{[{os.travel_price}]} </td>
                  <td>{[{os.real_arl_price}]} </td>
                  <td>{[{os.arl_value_timene}]} </td>
                  <td>{[{os.travel_quantity}]} </td>
                    {{Form::open(array('action'=>'OSbillingsController@postStore'))}}
                  <td><input  class="form-control" type="number" :max="os.possible_hours" min="1" v-model="os.input_hours"> </td> 

                  <!-- Calculo facturacion total -->
                  <td>
                     {[{(os.input_hours * os.real_arl_price) +(os.arl_value_timene*os.hours_not_efective) +(os.travel_quantity*os.travel_price) }]} 
                    <input 
                      class="form-control" 
                      type="hidden"  min="0" v-model="os.input_price"> </td>


                  <td><input type="checkbox" v-model="os.selected"></td>
                </tr>
                <tr>
                  <td colspan="9" class="text-right">
                    <button class="btn btn-default" @click="bill">Crear facturas</button>
                  </td>
                </tr>
          </table>
            {{Form::close()}}
          </div>
        </div>
        </div>
      </div>
    </div>
  </div>
</div>
  

这是vue js

<script src="https://unpkg.com/vue@2.4.4/dist/vue.js "></script>
  <script src="https://cdn.jsdelivr.net/npm/vue-resource@1.3.4"></script>
<script>

  var app = new Vue({
  el: '#app',
  data: {
    term: '',
    ordersFound: [],
    ordersSelected: [],
    numeroFactura: '',
    fechaFactura: '',


  },

  methods: {
    search: function () {
      this.$http.post('http://safe.potatokoosh.com/cpanel/order/billings/ordersearch', {term: this.term})

      .then(ok => {
        this.ordersFound = ok.body

        if (ok.body.length < 1) {
          alert("No se encontró nada facturable.")

        }
      })
    },
    calcPrice: function (order, index) {
      order = order[index]
      let arl_price = (parseInt(order.real_arl_price) * this.ordersSelected[index].input_hours) 
      let timene_price = (parseInt(order.arl_value_timene) * parseInt(order.hours_not_efective)) 
      this.ordersSelected[index].input_price = ((this.ordersSelected[index].possible_hours * order.real_arl_price) +(order.arl_value_timene*order.hours_not_efective) +(order.travel_quantity*order.travel_price))
    },
    add: function (order) {
      var found = 0
      for(var i = 0; i < this.ordersSelected.length; i++) {
        if (this.ordersSelected[i].id === order.id) {
          found++
          break
        }
      }
      if (found === 0) {
        this.ordersSelected.push(order)
      }
      this.calcPrice(this.ordersSelected, found);
    },
    bill: function (order, index) {
      if (this.numeroFactura === '') {
        return alert("Por favor escribe número de factura")
      }
      if (this.fechaFactura === '') {
        return alert("Por favor escribe fecha de factura")
      }
      var obj = {};
      for(var i = 0; i < this.ordersSelected.length; i++) {
        if (this.ordersSelected[i].selected !== true) {
          continue
        }
        if (this.ordersSelected[i].input_hours < 1) {
          this.ordersSelected[i].selected = false
          continue
        }
        if (this.ordersSelected[i].input_price < 1) {
          this.ordersSelected[i].selected = false
          continue
        }

        obj = {
          'order_id': this.ordersSelected[i].id,
          'billing_number': this.numeroFactura,
          'billed_hours': this.ordersSelected[i].input_hours,
          'price': this.ordersSelected[i].input_price,
          'created_at': this.fechaFactura,
          'type': '1'
        },

        console.log(obj)

       this.$http.post('http://safe.potatokoosh.com/cpanel/order/billings/postStorefast')
      .then(function (response) {
        that.ordersSelected[i] = response.data;
      }).
      catch(function (error) {
       console.log('Error: ' + error)});

}

      this.ordersSelected = []
      this.ordersFound = []
      this.term = ''
      this.numeroFactura = ''
      this.fechaFactura = ''
    }
  },
  delimiters: ["{[{","}]}"]
})
</script>

1 个答案:

答案 0 :(得分:0)

您可以仅在request中在主体请求中添加json(即您的v模型),但请求必须为POST,PUT和PATCH。

在axios中,您会执行以下操作

axios.post('/users', this.user).then(response => response);

在您的控制器中,您将像数组一样接收它,因为laravel解析对象。 这样就可以得到

$request->user['name'] //for example