Insertion with relationship N:N

Asked

Viewed 100 times

0

inserir a descrição da imagem aqui

I have these text inputs, in a way people can choose to fill them or not. Or just fill in some of them. I send the data from this form via Xios in an array (I don’t know if it’s the best way, so I’m here doing this post). Explaining further, this form would be the taxes(Taxes) which is part of a product form(Products).

And for this I create 3 tables in the Standard. A table for Products, another to the Taxes_names a pivot table, relating the id of both but with an additional field, which in this case is the value, that would store the value placed in those input text.

In the Laravel, I request this array as follows.

$taxes = $request -> taxes;

The interface is mounted and correct. I can enter the data via quiet database and do a debug, etc.

Pivot table product_taxes: id | product_id |taxes_id

Let’s say on the table taxes_name has the following data:

id | name |
-----------------------
1  | cfop |
2  | cst/csosn icms|
3  | cst pis(danfe)|
4  | cst cofins(danfe)|

My problem is, how will I pull the ids correctly from this table and move to the pivot table, along with these filled Forms.

Model da class Products:

public function taxes()
{
    return $this->belongsToMany('App\TaxesNames', 'product_taxes', 'product_id', 'taxes_id')->withPivot('value');

}

Array received in the Laravel

array (
    'cfop' => '12',
    'cst' => '2',
    'cstPis' => '3',
    'cstCofins' => '4',
  )

Xios:

           this.axios.post('http://chart.solutions/public/api/produto', {
                nome: this.nome, 
                codigo: this.codigo, 
                preco: this.preco,
                group: this.selectedgroup,
                subgroup: this.selectedsubgroup,
                taxes: this.taxes
            })
            .then((response) => {
                if(response.data.success){
                    this.clearForm()
                    this.productSwal("success")
                    console.log(this.taxes)
                }
            })
            .catch((error) => {
                    this.productSwal("error") 
            });
  • 2

    Well, what are you doing to enter the data? Where’s the transport layer (in your case, the Axios code you’re using)?

  • So, I send this data as an array. I edited the post here and posted how the Variable receives this array. By the way, can I just get the "key" separated from the value in this array? If I can, I probably have a stop in my head to solve this problem. I think I pull his name, I search the db and return the id, and so on.

  • 1

    Where is Axios? You must also put in the code.

  • edited. I found a solution, maybe not very feasible. I take the Keys from that array, and look for it in the bank with a Where. Then I get the id and think that’s it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.