值未保存到laravel中的数据库中

时间:2018-09-30 09:40:53

标签: php database laravel laravel-5 csrf

我正在尝试保存我的复选框值,但我不知道我在哪里错了,我陷入了这一问题 我正在执行动作的控制器:

控制器:

public function preferences(Request $request)
{
    $user = Auth::guard()->user();

    $preference = new Preference();

    $preference->userID = $user;
    $preference->monthly = Input::get('monthly') == 'on' ? 1 : 0;
    $preference->weekly = Input::get('weekly') == 'on' ? 1 : 0;
    $preference->tax_reviews = Input::get('tax_reviews') == 'on' ? 1 : 0;
    $preference->quarterly = Input::get('quarterly') == 'on' ? 1 : 0;
    $preference->quarterly = Input::get('quarterly') == 'on' ? 1 : 0;
    $preference->annually = Input::get('annually') == 'on' ? 1 : 0;
    $preference->newsletter = Input::get('newsletter') == 'on' ? 1 : 0;
    $preference->direct_mail_reviews = Input::get('direct_mail_reviews') == 'on' ? 1 : 0;
    $preference->email = Input::get('email') == 'on' ? 1 : 0;
    $preference->sms = Input::get('sms') == 'on' ? 1 : 0;
    $preference->save();

    return back();

}

我的表格:

<form id="preferences_form" role="form" method="POST" novalidate action="{{ url('/preference') }}" class="mujucet-registration">
                                <h5>Nastavení zpráv</h5>
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label  for="ch1">
                        <input type='hidden' name='monthly' value='0'>
                                                <input type="checkbox" name="monthly" value="1" id="ch1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Měsíční výpisy
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label>
                        <input type='hidden' name='monthly' value='0'>
                                                <input type="checkbox" name="weekly" value="1" ><span class="cr"><i class="cr-icon fa fa-check"></i></span>Týdenní přehled
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label>
                        <input type='hidden' name='tax_reviews' value='0'>
                                                <input type="checkbox" name="tax_reviews" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Daňové výpisy
                                            </label>
                                        </div>
                                    </div>

                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label>
                        <input type='hidden' name='quarterly' value='0'>
                                                <input type="checkbox" name="quarterly" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Čtvrtletní výpisy
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label>
                        <input type='hidden' name='annually' value='0'>
                                                <input type="checkbox" name="annually" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Roční výpisy
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label>
                        <input type='hidden' name='newsletter' value='0'>
                                                <input type="checkbox" name="newsletter" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Novinky
                                            </label>
                                        </div>
                                    </div>

                                    <div class="col-md-12">
                                        <div class="checkbox">
                                            <label class="border">
                        <input type='hidden' name='direct_mail_reviews' value='0'>
                                                <input type="checkbox" name="direct_mail_reviews" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span> Chci výpisy zasílat také Poštou <span class="light">20 kč za výpis</span>
                                            </label>
                                        </div>
                                    </div>

                                </div><h5>Kontaktní preference</h5>
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label>
                        <input type='hidden' name='email' value='0'>
                                                <input type="checkbox" name="email" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Email
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="checkbox">
                                            <label>
                        <input type='hidden' name='sms' value='0'>
                                                <input type="checkbox" name="sms" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>SMS
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </form>                        </div>

我的路线

Route::post('preference','HomeController@preferences');

我拥有数据库名称的模型

class Preference extends Model
{
    protected $table = 'db_preferences';

    protected $fillable = ['userID','monthly','weekly','tax_reviews','quarterly','annually','newsletter','direct_mail_reviews','email','sms'];
}

我不知道为什么数据没有插入数据库中,我单击了提交按钮并刷新了页面,这没错,但是数据没有保存到数据库中。

我们将非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我相信这里的问题可能缺少CSRF令牌。您应该添加

{{ csrf_field() }}

之后

<form id="preferences_form" role="form" method="POST" novalidate action="{{ url('/preference') }}" class="mujucet-registration">

答案 1 :(得分:0)

编辑1:还要考虑Marcin的Nabiałek答案! Csrf令牌丢失。.

为了了解会发生什么并实际学习,请在进行任何更改之前检查您从请求中获得的输入。在函数public void setDOB(String dateOfBirth) throws InvalidPersonException { try{ Date date=new Date(dateOfBirth); this.dateOfBirth=date.getDate(); } catch(InvalidDateException x) { throw new InvalidPersonException("Invalid Date of Birth: <"+dateOfBirth+"- "); } 的第一行中添加命令dd($request->all());,以查看preferences()所持有的inputs

有两个问题。

1)HTML。由于您拥有

$request

您不需要隐藏的类型:

<input type='checkbox'>

例如代替:

<input type='hidden'>

将其更改为(无需输入类型,因为它是不需要的):

 <input type='hidden' name='newsletter' value='0'>
 <input type="checkbox" name="newsletter" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Novinky

2)其次,控制器检查值 <input type="checkbox" name="newsletter" value="1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Novinky ,但您在复选框上输入的值是on,因此应将其更改为:

1

*如果未设置复选框值,但用户已选中该复选框,则它将返回值$preference->newsletter = Input::get('newsletter') == 1 ? 1 : 0;

有关情况(1)的其他信息:

您不应有两个元素具有相同的on。实际的问题是您检查的值为name,但是输入的值为on。即使具有相同名称的元素存在两次,该请求也将被最后一个覆盖(在您的情况下,其输入类型复选框将被覆盖)。但是,根据您的情况,应删除隐藏的输入类型。