Laravel: how to get data with post

Viewed 303

I've created a function in my routes that take some data from a view and send to another view

Route::post('/trans',  function(){
    $j = Input::get('r');
    return view('movs.create')->with($j);
});

this route take the data from this form

<form action="/trans" method="POST">
                    @csrf
                    <div class="input-group">
                      <input type="hidden" class="form-control" name="r" value={{$cooperado->id}}>
                        <button type="submit" class="btn btn-primary">
                          <span>+</span>
                        </button>
                      </span>
                    </div>
                </form>

but cant set the data in this other form on 'movs.create'

<form method="post" action="{{ route('movs.store') }}">
          <div class="form-group">
              @csrf
              <label for="name">ID COOP:</label>
              <input type="number" class="form-control" name="id_coop" readonly/> <-- data must be setted here
          </div>
          <div class="form-group">
              <label for="price">VALOR MOVIMENTACAO:</label>
              <input type="number" step=0.01 class="form-control" name="valor"/>
          </div>
          <button type="submit" class="btn btn-primary">Add</button>
      </form>

when i try to set the data in id_coop input, laravel says that the variable doesnt exists

3 Answers

To set data in the create form, you may need to add a value attribute to the id_coop input:

<input type="number" class="form-control" name="id_coop" value="{{ $j }} readonly/>

Also, ->with() needs to be a key (variable name) and value:

Route::post('/trans',  function(){
    $j = Input::get('r');
    return view('movs.create')->with('id_coop', $j);
});

This would mean you use {{ $id_coop }} instead.

with works with key value pair

Route::post('/trans',  function(){
    $j = Input::get('r');
    return view('movs.create')->with('j',$j);
   // or return view('movs.create', compact('j')); // it will extract in 
    //blade as $j
    // or return view('movs.create', ['j' => $j]); 
});

// you can fetch that data in blade as {{$j}}

<input type="number" class="form-control" name="id_coop" value="{{$j ?? ''}}" readonly/> 

Example of with,

return view('greeting')->with('name', 'Victoria'); // name as key and Victorial as value.

{{$j ?? ''}} if data is not set then '' value.

//controller

public function Postdata(Request $request){

        $data['j'] = Input::get('r');
        return view('movs.create',$data);


    }

//route

Route::post('/trans','yourController@Postdata');

//your view

<form method="post" action="{{ url('/store') }}">
          <div class="form-group">
              @csrf
              <label for="name">ID COOP:</label>
              <input type="number" class="form-control" name="id_coop" value="{{ $j }}" readonly/> <-- data must be setted here
          </div>
          <div class="form-group">
              <label for="price">VALOR MOVIMENTACAO:</label>
              <input type="number" step=0.01 class="form-control" name="valor"/>
          </div>
          <button type="submit" class="btn btn-primary">Add</button>
      </form>

//store route

Route::post('/store','yourController@Savedata');

hope this helps

Related