【问题标题】:Recover data from a select and send it to the database从选择中恢复数据并将其发送到数据库
【发布时间】:2020-09-15 03:46:28
【问题描述】:

我最近刚开始使用laravel,遇到了一个问题。

我在“表单”中有一个“选择”,我想检索选定的数据并将其保存在数据库中。

很遗憾,我的印象是数据没有被选中

这里是选择:

 <div class="input-group mb-3 ">
        <select class="custom-select @error ('category') is-invalid @enderror" id="category">
            @foreach ($categories as $category)
            <option value={{ $category->id}}>{{ $category->name}}</option>
            @endforeach
        </select>
        @error('category')
            <div class="invalid-feedback">
                {{$errors->first('category')}}
            </div>
        @enderror
    <div class="input-group-append">

这里是控制器:

$request->validate([
        'title' => 'required|min:5',
        'content' => 'required|min:10',
        'image' => 'mimes:jpg,jpeg,png,gif,bmp',
        'category' => 'required'
    ]);

    $topic = new Topic();

    $topic->title = $request->input('title');
    $topic->content = $request->input('content');

    $id = DB::table('categories')->where('name', $request->input('category'))->value('id'); //get the category id
    $topic->categorie_id = $id;

这是错误:

bugSelect

【问题讨论】:

    标签: php html sql laravel


    【解决方案1】:

    您需要将 name="category" 添加到您的 &lt;select&gt;,否则它不是通过表单发送的。

    <select name="category" class="custom-select @error ('category') is-invalid @enderror" id="category">
        @foreach ($categories as $category)
        <option value={{ $category->id}}>{{ $category->name}}</option>
        @endforeach
    </select>
    

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2013-03-02
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多