【问题标题】:Laravel store doesn't save anythingLaravel 商店不保存任何东西
【发布时间】:2021-03-22 11:58:33
【问题描述】:

我正在 Laravel 中进行应用程序。我创建了一个表单来保存模型(修复)。但是当我保存表单时,您会看到它正在重定向,但没有任何保存。没有显示错误或成功消息?

RepairController.php中的store函数:

public function store(Request $request)
    {
        $this->validate($request, [
            'customer_id' => 'required',
            'defect' => 'required',
            'stats_id' => 'required'
        ]);

        Repair::create([
            'customer_id' => $request->customer_id,
            'serial_number' => $request->serial_number,
            'model' => $request->model,
            'defect' => $request->defect,
            'diagnostics' => $request->diagnostics,
            'comments' => $request->comments,
            'status_id' => $request->status_id 
        ]);
        return redirect()->route('repair.index')->with('success',$msg);
    }

Repair.php 有一个可填充的:

protected $fillable = ['customer_id', 'serial_number', 'model', 'defect', 'diagnostics', 'comments', 'status_id'];

创建模式:

<div id="newModal" class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
  <div  class="modal-dialog modal- modal-dialog-centered modal-" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h6 class="modal-title" id="modal-title-default">Type your modal title</h6>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>

      <div class="modal-body">
        <form action="{{ route('repair.store') }}" method="POST" id="editForm">
          @csrf
          <input type="text" name="customer_id" class="form-control" placeholder="Customer ID"><br>
          <input type="text" name="serial_number" class="form-control" placeholder="Serial number"><br>
          <input type="text" name="model" class="form-control" placeholder="Model"><br>
          <input type="text" name="defect" class="form-control" placeholder="Defect"><br>
          <div class="form-group">
            <label for="Diagnostics">Diagnostics</label>
            <textarea name="diagnostics" class="form-control" id="Diagnostics" rows="3"></textarea>
          </div>
          <div class="form-group">
            <label for="Comments">Comments</label>
            <textarea name="comments" class="form-control" id="Comments" rows="3"></textarea>
          </div>
          <input type="text" name="status_id" class="form-control" placeholder="Status ID"><br>
      </div>

      <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Create repair</button>
        <button type="button" class="btn btn-link  ml-auto" data-dismiss="modal">Close</button>
      </div>
        </form>
    </div>
  </div>
</div>

web.php中的路由:

Route::resource('repair', 'RepairController');

【问题讨论】:

  • 任何错误信息?你检查你是否点击了商店功能?
  • 否,显示 nog 错误或成功消息。如何检查它是否命中了商店功能?
  • 将 dd('something') 添加到函数中。然后尝试提交表单,看看会不会触发dd。
  • 您的刀片文件中是否显示任何错误?您使用$msg 重定向,但它从未设置。之后使用$repair = Repair::create...dd($repair)。您的 db 字段是否正确设置为可以为空,以防万一

标签: laravel crud


【解决方案1】:

您的数据中可能存在一些验证失败。您能否在表单上方添加以下代码并查看打印的内容。

@if($errors->any())
  <ul>
    @foreach($errors->all() as $error)
      <li>{{$error}}</li>
    @endforeach
  </ul>
@endif

【讨论】:

  • 谢谢。我在验证中犯了一个错字。由于这种错误处理,我想通了。有 'stats_id' => 'required' 而不是 'status_id' => 'required' 哎呀
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多