How to store boolean values ​​in laravel eloquent

I did the following migration to Laravel:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class QualityCheckTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('quality_check', function (Blueprint $table) {
            $table->increments('id');
            $table->boolean('favicon');
            $table->boolean('title');
            $table->boolean('image-optimization');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('quality_check');
    }
}

I have the following controller method that starts when a form is submitted in frontEnd:

public function store(CreateArticleRequest $request) {
        // $input = Request::all();
        Article::create($request->all());
        return redirect('articles');
    }

My form looks like this:

{!! Form::open([ 'action' => 'QualityCheckController@validateSave' , 'class'=>'quality-check-form' , 'method' => 'POST' ]) !!}

        <div class="input-wrpr">
            {!! Form::label('favicon', 'Favicon') !!}
            {!! Form::checkbox('favicon', 'value' ); !!}
        </div>

        <div class="input-wrpr">
            {!! Form::label('title', 'Page Title') !!}
            {!! Form::checkbox('title', 'value'); !!}
        </div>

        <div class="input-wrpr">
            {!! Form::label('image-optimization', 'Image Optimization') !!}
            {!! Form::checkbox('image-optimization', 'value'); !!}
        </div>

        {!!  Form::submit('Click Me!') !!}

    {!! Form::close() !!}

So, when the method starts, the values ​​of the flags are stored in the database.

At the moment, all entries are displayed as 0. For instance:

enter image description here

Now, how to make it so that when the checkbox is checked, it is 1saved, and if the checkbox is not checked, the value in remains in 0??

+4
source share
4 answers

, . , . , $request->all() . , , 3 , $request->all() (, ).

Article::create($request->all()); , , , , .

, MySQL , , 0. /.

, 1 , 0, . - 1 i.e.

:

$table->boolean('favicon')->default(0);

:

{!! Form::checkbox('title', '1'); !!}

, , $request->all() , 'title' => '1', . 0.

, , , .

$article = new Article();
$article->title = $request->has('title'); // Will set $article->title to true/false based on whether title exists in your input
// ...
$article->save();
+7

, $fillable ?

protected $fillable = [
    'favicon',
    'title',
    'image-optimization'
];

. . , , , , true, :

    <div class="input-wrpr">
        {!! Form::label('title', 'Page Title') !!}
        {!! Form::hidden('title', false); !!}
        {!! Form::checkbox('title', 'value'); !!}
    </div>
+4

, isset($request->favicon) empty($request->favicon) ->has('favicon'):

public function store(CreateArticleRequest $request) {
    foreach (['favicon', 'title', 'image-optimization'] as $box) {
        $request($box) = $request->has($box);
    }
    Article::create($request->all());
    return redirect('articles');
}
+2

Source: https://habr.com/ru/post/1663373/


All Articles