How do I retrieve and calculate 2 numbers via POST on the Controller in Laravel?

2

I'm a beginner in language and I'm trying to learn by myself, I created a .blade.php file in order to fill two simple fields and return the calculation via submit, after that I want to list a history stored in the SQL database, but I barely know how to retrieve information of the INDEX file !!! So you can realize that I'm well lost xD ... and all help and advice will be very useful in my case, especially if I'm doing it WRONG !!! Here are the files:

HTML:

<html>
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    </head>

    <div class='section'>
        <div class='container'>
            <div class='row'>    
                <div class='col-sm-3'>
                    <form method='POST'>
                        <input type='text' name='elemento1'>
                        <input typle='text' name='elemento2'><br>
                        <a href="calcular"><button type='submit'>Calcular</button></a><br>
                        <input typle='text' id='elemento2'>
                    <form method='POST'>
                </div>
            </div>
        </div>
    </div>
</html>

ROUTE:

<?php

Route::get('/', 'admin\DashBoardController@index');

Route::prefix('/')->group(function(){
    Route::post('calcular','admin\DashboardController@calcular');
});

CONTROLLER:

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DashboardController extends Controller
{
    public function index(){
        return view('index');
    }
    public function calcular(Request $requisicao){

        return $_POST;
    }
}
    
asked by anonymous 11.10.2018 / 16:12

2 answers

0

First replace your blade from the section with this code here:

<div class='section'>
    <div class='container'>
        <div class='row'>    
            <div class='col-sm-3'>
                <form method='POST' action='/calcular'>
                    <label>valor 1 :</label><input type='text' name='valor1'><br />
                    <label>valor 2 </label><input typle='text' name='valor2'><br />
                    <input type="submit" value="Enviar" /><br>
                </form>
            </div>
        </div>
    </div>
</div>

Any POST requestion you make must have an action ( one way you want the data to be received ). The values you want to get will always have to be within the form tag, preferably the inputs should be named.

Replace the calculate method in your DashboardController with this code snippet:

public function calcular(Request $requisicao){

        $valor1 = $requisicao->input('valor1');
        $valor2 = $requisicao->input('valor2');

        return response()->json(['valor1 : ' => $valor1, 'valor2' => $valor2]);
    }

Using $ _ POST is not wrong, but you are using Laravel a php framework, so you can abstract this using the class Request through the method of the same class input('valor1') (which takes the value of the input name set on your blade).

    
11.10.2018 / 16:32
1

Good morning friend, it's very simple to work with laravel, I highly recommend you learn the framework by reading the documentation .

1st - Your form is set in the wrong way. This would be the correct way to create, of course for your application.

<form method="POST" action="{{route('calcularNumero')}}">
  <input type='number' name='elemento1'>
  <input type='number' name='elemento2'><br>
  <a href="calcular">
    <button type='submit'>Calcular</button>
   </a><br>
  <input type='text' id='elemento2'>
</form>

Remember that Action property is defined on which route your request will go, where all the information that is on your form will go.

I recommend reading this documentation about the form tag.

2º- I put a name for your controller so you can access it in a better way in the view.

Route::prefix('/')->group(function(){
    Route::post('calcular','admin\DashboardController@calcular')->name('calcularNumero');
});

The name property serves to name your routes so you can access it easily.

The laravel documentation speaks better about routes .

3º- Defining the way you can get the values that come from your form.

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DashboardController extends Controller
{
    public function index(){
        return view('index');
    }
    public function calcular(Request $request){//Retirei o $requisição e coloqei $request 

      $numeroUm = $request->input('elemento1');
      $numeroDois = $request->input('elemento2');
      $calq = $numeroUm + $numeroDois;
      return $calq;
     //ou
    /*
     *response()->json($calq);
     */
    }
}

Laravel documentation that best explains how the Controller works , and how Request / Requests works.

    
11.10.2018 / 16:35