【问题标题】:Form in Laravel don't send dataLaravel 中的表单不发送数据
【发布时间】:2018-12-03 06:55:15
【问题描述】:

我尝试制作一个包含多个日期输入的表单,我从注册/登录刀片模板中复制了模型,但它不起作用,这是我在模板中所做的:

<form method="POST" action="{{ route('createDispo') }}">
    @csrf

    <div class="form-group row">
        <label for="date_debut" class="col-md-4 col-form-label text-md-right">Date de début</label>

        <div class="col-md-6">
            <input type="date" name="date_debut" class="form-control" required>
        </div>
    </div>
    <div class="form-group row">
        <label for="date_debut_heure" class="col-md-4 col-form-label text-md-right">Heure de début</label>

        <div class="col-md-6">
            <input type="date" name="date_debut_heure" class="form-control" required>
        </div>
    </div>
    <div class="form-group row">
        <label for="date_fin" class="col-md-4 col-form-label text-md-right">Date de fin</label>

        <div class="col-md-6">
            <input type="date" name="date_fin" class="form-control" required>
        </div>
    </div>
    <div class="form-group row">
        <label for="date_fin_heurev" class="col-md-4 col-form-label text-md-right">Heure de fin</label>

        <div class="col-md-6">
            <input type="date" name="date_fin_heure" class="form-control" required>
        </div>
    </div>

    <div class="form-group row mb-0">
        <div class="col-md-6 offset-md-4">
            <button type="submit" class="btn btn-primary">
                Ajouter
            </button>
        </div>
    </div>
</form>

但是表单不像注册或登录那样返回$data数组,而只返回第一个值(date_debut而不是数组,如何正确使用Laravel做一个表单?

编辑:

这是我的控制器

public function createDispo(array $data){
    $disponibilite = new Disponibilite();
    $disponibilite->date_debut = $data["date_debut"];
    $disponibilite->date_fin = $data["date_fin"];
    //$user->disponibilites()->save($disponibilite);
}

问题是我没有收到这个“数据”数组,但只有一个值(第一个日期选择器)

【问题讨论】:

  • 您好,这可能与您在控制器中处理请求的方式有关,您可以在这里发布吗?
  • 我编辑我的消息

标签: laravel forms post


【解决方案1】:

您可以使用$request 访问提交的数据。 The docs describe it well。在你的情况下:

use Illuminate\Http\Request;

public function createDispo(Request $request){
    $disponibilite = new Disponibilite();
    $disponibilite->date_debut = $request->date_debut;
    $disponibilite->date_fin = $request->date_fin;
    // ...
}

【讨论】:

    【解决方案2】:

    当您以这种形式发送 HTTP 请求时,应该获取此信息的方法应该接收 Illuminate/Http/Request 类。

    控制器登录的做法不同,因为控制器在验证后将 $request->all()(这是一个数组)发送到方法登录。

    所以你要做的就是改变:

    public function createDispo(array $data){
    

    public function createDispo(Request $data){
    

    或者更一致的变量:

    public function createDispo(Request $request){
    

    然后您可以通过以下方式访问数据:

    public function createDispo(Request $request){
    
        $disponibilite = new Disponibilite();
        $disponibilite->date_debut = $request->date_debut;
        $disponibilite->date_fin = $request->date_fin;
        //$user->disponibilites()->save($disponibilite);
    }
    

    使用Request的时候记得导入Illuminate\Http\Request的类

    【讨论】:

    • 该错误意味着您正在使用未在您的路由中注册的特定方法发出请求,例如,如果在您的路由文件中创建 Route::get('create_dispo', .. .....);您将收到错误,因为您的表单正在通过 POST 发送数据,并且路由定义为 GET
    猜你喜欢
    • 2020-11-12
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    相关资源
    最近更新 更多