我在我的路由中创建了一个函数,它从一个 View 中获取一些数据并发送到另一个 View

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

这条路线从这个表格中获取数据
<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>

但不能在 '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>

当我尝试在 id_coop 输入中设置数据时,laravel 说该变量不存在

最佳答案

with 使用键值对

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]);
});

//您可以在 Blade 中获取该数据作为 {{$j}}
<input type="number" class="form-control" name="id_coop" value="{{$j ?? ''}}" readonly/>
with 示例,
return view('greeting')->with('name', 'Victoria'); // name as key and Victorial as value.
{{$j ?? ''}} 如果未设置数据,则为 '' 值。

关于php - Laravel:如何通过帖子获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56495140/

10-09 08:13
查看更多