Ad

Values Are Not Saving Into Database In Laravel

- 1 answer

I am trying to save my checkbox value but i dont know where i am wrong i am stuck in this issue my controller where i am performing my action:

Controller:

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();

}

My Form:

<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>

My routes

Route::post('preference','[email protected]');

My model where i am having my database name

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

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

i dont know why data is not inserting into database i click the submit button and page refresh and thats it and i am getting no error but data is not saving into db.

Your help will be highly appreciated!

Ad

Answer

I believe the problem here might be missing CSRF token. You should add

{{ csrf_field() }}

after

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

line

Ad
source: stackoverflow.com
Ad