Php laravel使用post方法通过控制器

时间:2016-04-30 03:23:57

标签: php laravel

我尝试将值表单从post方法发送到控制器

这是我的观点,我如何使用post方法发送

 <form class="form-horizontal" role="form">
    <div class="form-group">
      <label class="col-lg-3 control-label">Title:</label>
      <div class="col-lg-8">
        <input class="form-control" value='{{ $words->first()->title }}' type="text">
      </div>
    </div>
    <div class="form-group">
      <label class="col-lg-3 control-label">Meaning:</label>
      <div class="col-lg-8">
        <input class="form-control" value="{{ $words->first()->meaning }}" type="text">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-3 control-label"></label>
      <div class="col-md-8">
        <input class="btn btn-primary" value="Save Changes" type="button">
        <span></span>
        <input class="btn btn-default" value="Cancel" type="reset">
      </div>
    </div>
  </form>

这里是控制器方法,如

public function postSaveedit($meaning){


}

按控制器使用路径

3 个答案:

答案 0 :(得分:1)

您应该阅读Laravel中的请求:https://laravel.com/docs/5.2/requests#accessing-the-request

您需要将其传递给您的控制器

var y []uint16

答案 1 :(得分:0)

这里你去

<强>表格

您必须在表单+名称中添加方法

<form class="form-horizontal" role="form" method="POST">
    <!-- Add csrf token -->
    {!! csrf_field() !!}

    <div class="form-group">
      <label class="col-lg-3 control-label">Title:</label>
      <div class="col-lg-8">
        <input class="form-control" value='{{ $words->first()->title }}' type="text" name="input1">
      </div>
    </div>
    <div class="form-group">
      <label class="col-lg-3 control-label">Meaning:</label>
      <div class="col-lg-8">
        <input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="input2">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-3 control-label"></label>
      <div class="col-md-8">
    <input class="btn btn-primary" type="submit" value="Save Changes"/>
        <span></span>
        <input class="btn btn-default" value="Cancel" type="reset">
      </div>
    </div>
  </form>

<强>控制器

Use Word; // at the top of the class
public function postSaveedit(Request $request) {
$word= new Word; // if you are creating a new record
$word= Word::find(1);// if you are updating a record

$word->title = $request->input('input1');
$word->meaning= $request->input('input2');
$word->save();

return view('home.blade.php');

}

路由文件

Route::get('/myurl', 'Controllername@postSaveedit');

:)

答案 2 :(得分:0)

在Laravel 5.2中,您可以使用request()辅助方法来解决您的问题...... 这就是你可以做到的......

路由文件应如下所示(确保此路由应为post类型)

Route::post('/myurl', 'Controllername@postSaveEdit')->name('postSaveEdit');

表单文件应该如下所示,也请在表单中指定输入字段名称,以便您可以通过指定的名称在控制器中获取它们(例如 - 标题,含义 - 请参阅下面的代码)...

<form class="form-horizontal" action="{{ route('postSaveEdit') }}" method="POST" role="form">
  <div class="form-group">
    <label class="col-lg-3 control-label">Title:</label>
    <div class="col-lg-8">
      <input class="form-control" name="title" value='{{ $words->first()->title }}' type="text">
    </div>
  </div>
  <div class="form-group">
    <label class="col-lg-3 control-label">Meaning:</label>
    <div class="col-lg-8">
      <input class="form-control" name="meaning" value="{{ $words->first()->meaning }}" type="text">
    </div>
  </div>

  <div class="form-group">
    <label class="col-md-3 control-label"></label>
    <div class="col-md-8">
      <button class="btn btn-primary" type="submit">Save Changes</button>
      <span></span>
      <input class="btn btn-default" value="Cancel" type="reset">
    </div>
  </div>
</form>

和控制器应该是这样的......

public function postSaveEdit() {
    // The inputs variable contains all your form's inputs in the form of array...
    $inputs = request()->all();
    /* 
        $inputs = array(
            'title' => 'title_value',
            'meaning' => 'meaning_value'
        )
    */
    // Wheareas you can also get them by using 'get' method on request method like this
    $title = request()->get('title');
    $meaning = request()->get('meaning');
}