【问题标题】:No error with Fail Save and show 302 post error... How To solve it?失败保存没有错误并显示 302 发布错误...如何解决?
【发布时间】:2020-06-28 16:06:28
【问题描述】:

<div class="row">
    <div class="col-md-6">
      <h3>Create New Expenses</h3>
        @if ($errors->any())
          <div class="alert alert-danger">
            <ul>
              @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
              @endforeach
            </ul>
          </div>
        @endif
      <form class="form" method="POST" action="{{ route('exp.store') }}" autocomplete="off">
    <div class="form-group">
        <label>Seller :</label>
        <input type="text" name="seller_name" class="form-control" value="{{ old('seller_name',isset($exp) ? $exp->seller_name : '') }}" placeholder="Seller">
    </div>
    <div class="form-group">
        <label>Date :</label>
        <input type="date" name="date" class="form-control" value="{{ old('date',isset($exp) ? $exp->date : '') }}">
    </div>
    <div class="form-group">
        <label>Select Type :</label>
        <select class="form-control" name="category" value="">
            <option value=""> Select Type </option>
            <option value="1"{{ isset($exp) ? ($exp->category == 1 ? 'selected' : '') : '' }}> Communication </option>
            <option value="2"{{ isset($exp) ? ($exp->category == 2 ? 'selected' : '') : '' }}> Transport </option>
            <option value="3"{{ isset($exp) ? ($exp->category == 3 ? 'selected' : '') : '' }}> Tools </option>
            <option value="4"{{ isset($exp) ? ($exp->category == 4 ? 'selected' : '') : '' }}> Raw Material </option>
        </select>
    </div>
    <div class="form-group">
        <label>Amount(RM) :</label>
        <input type="number" name="amount" class="form-control" value="{{ old('amount',isset($exp) ? $exp->amount : '') }}" placeholder="0.00">
    </div>
    <br>
    <button type="submit" class="btn btn-primary btn-sm">Save</button>
    <a class="btn btn-danger btn-sm" href="{{ route('exp.index') }}">Cancel</a>
</form>
    </div>
  </div>

这是我的保存数据功能

    public function store(Request $request){
            $this->validate($request, [
                'seller_name' => 'required|max:50',
                'date' => 'required',
                'category' => 'required',
                'amount' => 'required'
            ]);

            DB::beginTransaction();

            try{
                $exp = new Exp();
                $this->saveData($exp, $request);

                DB::commit();

                return redirect()->route('exp.index');
            } catch (\Exception $ex){
                DB::rollback();
                return back()->withInput()->withErrors('Fail to save.');
            }
        }
    private function saveData($exp, Request $request){
            $exp->seller_name = $request->input('seller_name');
            $exp->date = $request->input('date');
            $exp->category = $request->input('category');
            $exp->amount = $request->input('amount');
            $exp->save();
        }

这是我的模特

namespace App;

use Illuminate\Database\Eloquent\Model;

class Exp extends Model
{
    protected $table = 'exp';
    public $primaryKey = 'exp_id';
}

我是使用这个 MVC 的新手....我不知道为什么不能保存,但我的所有代码看起来都没有错误。

我试图找到它,但我什么也没找到..

我需要一些帮助...

我已经提供了我的视图、控制器和模型

下面是我的web.php

Route::get('/exp/create', 'ExpController@create')->name('exp.create');

Route::post('/exp/store', 'ExpController@store')->name('exp.store');

TQ。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    似乎您忘记将批量分配字段添加到您的 Exp 模型中,因此这些字段没有保存在您的数据库中。将要批量分配的所有字段添加到模型中,它应该可以按预期工作:

    protected $fillable = ['field_1', 'field_2', .... 'etc']; 
    

    在官方documentation了解更多信息

    【讨论】:

    • 添加填充后同样无法保存
    • 您确定您的代码可以到达控制器中的 store() 方法吗?在你的方法的顶部和底部做一些 dd(),以确保你的代码完全执行。
    猜你喜欢
    • 2021-08-22
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2020-03-16
    • 2013-12-16
    • 2019-02-18
    相关资源
    最近更新 更多