Set value of another table in the column of the current table (search value by ID)

Asked

Viewed 44 times

1

I’ll get right to the point.

My sql table looks like this:

id, venda_id, produto_id, qtd_saida, preco_produto

I’d like to fill in the column preco_produto automatically when the product be saved.

Using Laravel migrates, I can create columns calculated with storedAs, but I don’t know if it is possible to pass a query inside storedAs. Example of what I have in mind below:

$table->decimal('valor_total', 8, 2)->storedAs('select preco from produtos where id=produto_id');

How to solve?

  • Create a Rigger in the bank that you want to do is not supported by Migration

  • How this Rigger is made, can make an example?

  • 1

    Create a Observer so that a data is saved in your model (which seems to me to be of relationship). But before posting the answer would like to know the name of the tables including the table of relationships

  • To complement the Erlon comment, follow the documentation link: https://laravel.com/docs/5.8/eloquent#Observers I also think it is the best solution

No answers

Browser other questions tagged

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