How to Insert Taking Data from FORM in Laravel 5.2?

Asked

Viewed 2,598 times

4

I’m looking at the Laravel documentation on dbquery, in the Insert case. I need to do Insert outside of the Resource method (manually.

Type:

DB::table('users')->insert(
    ['email' => '[email protected]', 'votes' => 0]
);

But I need to get the data from a form. How do I do this? I didn’t see it in the documentation.

2 answers

4

You need to point your form to the route where the destination is the function you will be entering, for example;

Your form:

<form method="post" action="{{ route('cadastro/novo') }}">
...
</form>

Your Route:

Route::post('cadastro/novo', 'UsuariosController@create');

No Controller UsuariosController you can use the class User to manipulate BD data because every model is linked to a table in the MAS database in the plural, for example;

model user -> table users

model cat -> table cats

use App\User;

class UsuarioController extends Controller {

// A função Request tera todos os dados enviados via post.
public function create(Request $request) {

    // Pega os dados enviado atraves do POST exceto o input _token 
    // que é enviado por padrão atraves do form.
    $dados = $request->except('_token');

    // A inserção acontece aqui...
    User::create($dados);
}

To search the database you can continue using the model you want because it already has several methods to facilitate manipulation.

$user = User::find(1); // Busca o usuario com o ID 1
$user = User::where('nome', 'oNome'); // Busca o usuario onde o campo "nome" seja "oNome".

In the case of udpate you can do;

User::where('id', 999)->update(['sobrenome' => 'novoSobreNome']);
// Buscara e atualizara o usuário com ID 999 substituindo o campo sobrenome com "novoSobreNome"

OR

$user = User::where('id', 999);
$user->idade += 1;
$user->save();

You can consult all available methods accessing the page of Eloquent of the Laravel.

  • But one of the fields is a code and I need to add 1 to this code before giving the Insert. How could I do this?

  • All models have methods to search for information such as where, then you first need to fetch the data you want in the database to perform the update and/or send the update directly.

  • 1

    I am already playing from the form action to the function below in the controller and it worked/; public Function writOrdem() { $major code = DB::table('ordemservicos')->max('code'); $major code = $major code + 1; Return $major code;

  • This is where you want Mysql itself already does when placing the column auto_increment.

  • @Marcosbirro yes, this way also works, see the change I made.

  • The field cannot be autoincrement. I already have the ID as the Prim and autoincrement key. I need to do it manually but by example with the model I believe it will work because I will take field by field and I can add one to the code (Which is not primary Heart).

  • I tested and is giving an error in $request.

  • Errorexception in Ordemservicocontroller.php line 293: Undefined variable: request

  • But it is stated?:

  • class Ordemservicocontroller extends Controller { protected $request; public Function __Construct(Request $request) { $this->request = $request; }

  • you do not need to define the object of Request´ como atributo da classe, use apenas como $request->`

Show 6 more comments

3


Controller:

$model = new Model;
$model->nome = $request->get('nome'); // Vem do Form
$model->email = $request->get('email'); // Vem do Form
$model->telefone = $request->get('telefone'); // Vem do Form

$model->save();
  • 1

    Now I get it. I’ll test it but this is what you need to know. Thanks.

Browser other questions tagged

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