Laravel:如何通过Post获取数据

时间:2019-06-07 13:18:20

标签: php laravel

我已经在路由中创建了一个函数,该函数从一个视图中获取一些数据并发送到另一个视图

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说该变量不存在

3 个答案:

答案 0 :(得分:3)

要在创建表单中设置数据,您可能需要在value输入中添加id_coop属性:

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

此外,->with()必须是键(变量名)和值:

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

这意味着您改为使用{{ $id_coop }}

答案 1 :(得分:1)

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

//您可以以{{$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 ?? ''}}(如果未设置数据,则为”值。

答案 2 :(得分:1)

//控制器

function main() {
    var endDate = new Date();
    var endRange = Utilities.formatDate(endDate, 'America/Chicago', 'YYYYMMdd');

    var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/XXX');
    var sheet = spreadsheet.getSheetByName('Data')  
    var report = AdsApp.report(
        'SELECT Date, CampaignName, AverageFrequency, Impressions, ImpressionReach ' +
        'FROM   CAMPAIGN_PERFORMANCE_REPORT ' +
        'WHERE  Impressions > 0 ' +
        'DURING 20190528,' + endRange);

    sheet.clearContents();
    report.exportToSheet(sheet);
}

//路线

public function Postdata(Request $request){

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


    }

//您的视图

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

//存储路线

<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>

希望这会有所帮助