Laravel VueJs在laravel控制器中来自axios请求的存储数组

问题描述 投票:0回答:1

我有一个laravel 5.7应用程序,其前端使用VueJS 2.0。我有两个具有多对多关系的表:commandes和produits。我目前正在尝试将数据传递到我的commandes表中,但出现此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `commande_produit` (`0`, `commande_id`, `produit_id`) values (3, , 0))

如果有人能告诉我如何解决甚至指向正确的方向,将不胜感激。

HTML模板:

<template>
   <div class="container">
       <div class="row justify-content-center mt-5" v-if="$gate.isAdminOrVendeur()">
           <div class="col-md-12">
               <form class="" @submit.prevent="envoyer()" >
               <table class="table table-bordered table-striped table-responsive">
                   <thead class="thead-dark">
                       <tr>
                           <th class="col-sm-4">Produit</th>
                           <th class="col-sm-2">Quantité</th>
                           <th class="col-sm-2">montant</th>
                           <th class="col-sm-2">Total</th>
                           <th class="col-sm-1"></th>
                           <th class="col-sm-1"></th>
                       </tr>
                   </thead>
                   <tbody>
                   <tr v-for="(commande, index) in commandes" :key="index">
                       <td>{{ commande.produit_id }}</td>
                       <td>{{ commande.quantite }}</td>
                       <td>{{ commande.montant }} F</td>
                       <td>{{ (commande.quantite * commande.montant).toFixed(2) }} F</td>
                       <td><a class="btn btn-info btn-block" @click="modifier(index)">Modifier</a></td>
                       <td><a class="btn btn-warning btn-block" @click="supprimer(index)">Poubelle</a></td>
                   </tr>
                   <tr>
                       <td colspan="3"></td>
                       <td><strong> F </strong></td>
                       <td colspan="2"></td>
                   </tr>
                   <tr>
                       <td>
                           <div class="form-group">                            
                           <select v-model="form.produit_id" name="produit_id" class="form-control">
                               <option v-for="produit in produits.data" :key="produit.id" v-bind:value="produit.id">{{ produit.designation }}</option>                                
                           </select>

                       </div>
                       </td>
                       <td><input type="text" class="form-control" v-model="form.quantite"></td>
                       <td><input type="text" class="form-control" v-model="form.montant"></td>
                       <td colspan="3"><a class="btn btn-primary btn-block" @click="ajouter">Ajouter</a></td>
                   </tr>
                   </tbody>
                   <tfoot>
                   <a class="button btn btn-xs btn-warning" @click="toutPoubelle">Tout à la poubelle</a>
                   <button class="button btn btn-xs btn-success" type="submit">Valider</button>
                   </tfoot>
               </table>
           </form>
           <div class="panel panel-danger" v-show="poubelle.length">
               <div class="panel-heading">Poubelle</div>
               <table class="table table-bordered table-striped table-responsive">
                   <thead>
                   <tr>
                       <th class="col-sm-4">Produit</th>
                       <th class="col-sm-2">Quantité</th>
                       <th class="col-sm-2">montant</th>
                       <th class="col-sm-2">Total</th>
                       <th class="col-sm-1"></th>
                       <th class="col-sm-1"></th>
                   </tr>
                   </thead>
                   <tbody>
                   <tr v-for="(commande, index) in poubelle" :key="index">
                       <td>{{ commande.produit }}</td>
                       <td>{{ commande.quantite }}</td>
                       <td>{{ commande.montant }} F</td>
                       <td>{{ (commande.quantite * commande.montant).toFixed(2) }} F</td>
                       <td><a class="btn btn-success btn-block" @click="retablir(index)">Rétablir</a></td>
                       <td><a class="btn btn-danger btn-block" @click="eliminer(index)">Supprimer</a></td>
                   </tr>
                   </tbody>
               </table>
               <div class="panel-footer">
                   &nbsp;
                   <div class="btn-group">
                       <a class="button btn btn-xs btn-success" @click="toutRetablir">Tout rétablir</a>
                       <a class="button btn btn-xs btn-danger" @click="toutEliminer">Tout supprimer</a>
                   </div>
               </div>
           </div>
           </div>            
       </div>
   </div>
</template>

我的Vue实例:

<script>
    export default {
        data () {
            return {                
            commandes: [],
            poubelle: [], 
            produits: {}, 
            form: new Form({                                  
                produit_id : '',                    
                quantite : '',                    
                montant: '',                   
            })       
            }
        },

        methods: {      
            ajouter() {
                this.commandes.push({produit_id: this.form.produit_id, quantite: this.form.quantite, montant: this.form.montant});
                this.form = {};
                this.commandes.sort(ordonner);
            },

            modifier(index) {
                this.form.produit_id = this.commandes[index].produit_id;
                this.form.quantite = this.commandes[index].quantite;
                this.form.montant = this.commandes[index].montant;
                this.commandes.splice(index, 1);
            },

            supprimer(index) {
                this.poubelle.push(this.commandes[index]);
                this.commandes.splice(index, 1);
                this.poubelle.sort(ordonner);
            },

            retablir(index) {
                this.commandes.push(this.poubelle[index]);
                this.poubelle.splice(index, 1);
                this.commandes.sort(ordonner);
            },

            eliminer(index) {
                this.poubelle.splice(index, 1);
            },

            toutPoubelle() {
                this.poubelle = this.poubelle.concat(this.commandes);
                this.poubelle.sort(ordonner);
                this.commandes = [];
            },

            toutRetablir() {
                this.commandes = this.commandes.concat(this.poubelle);
                this.commandes.sort(ordonner);
                this.poubelle = [];
            },

            toutEliminer() {
                this.poubelle = [];
            },

            loadProduits(){
                //if(this.$gate.isAdminOrComptable()){
                    axios.get("api/produit").then(({ data }) => (this.produits = data));
                //}
            },

            envoyer() {
                axios.post('api/commande', {commande: this.commandes});
                this.commandes = [];        
            },       
        },
        mounted() {
            this.loadProduits();
            console.log('Component mounted.')
        }
    }

    var ordonner = function (a, b) {
        return (a.commande.toUpperCase() > b.commande.toUpperCase())
    };
</script>

这是命令模型:

class Commande extends Model
{
    protected $fillable = ['commande'];

    public function produits()
    {
    return $this->belongsToMany('App\Models\Produit');
    }
}

和产品模型:

class Produit extends Model
{
    protected $fillable = ['designation'];

    public function commandes()
    {
        return $this->belongsToMany('App\Models\Commande');
    }
}

Commande控制器store()方法:

 public function store(Request $request)
    {

        $this->validate($request,[
            'commande' => 'required',
        ]);

        $commande = new Commande();

        $commande->produits()->attach([$request->commande]);

        $commande->save();

    }
javascript php laravel vue.js axios
1个回答
0
投票

某些事情可能无法按您的代码预期的那样工作:

  1. 您正在尝试将produits附加到不存在的模型上。

通过使用new Commande,您正在准备对象Commande,尚未将其保存到数据库中。您尚未附加任何内容,因为它还没有id

解决方案:使用Create代替New

  1. 方法attach()的使用不正确

根据文档(https://laravel.com/docs/7.x/eloquent-relationships#updating-many-to-many-relationships),attach()期望使用ID数组。您正在尝试传递自定义数组,因此该方法将不起作用。

解决方案:从$request->command中提取ID,然后使用attach()

© www.soinside.com 2019 - 2024. All rights reserved.