How to save checkbox value to database Laravel

10,864

Solution 1

You can use request object to get the warranty input as:

$request->get('warranty')

or

$request->has('warranty')

Solution 2

 $request->merge(array('checkbox_name' => $request->has('checkbox_name') ? true : false));
Object::create($request->all());

This is how I save boolean parameters which are checkboxes in form.

Or simply give your checkbox value

 {{ Form::checkbox('checkbox_name', 1) }}

Solution 3

Input facade was removed in latest verison, so you can add it to config/app.php:

'Input' => Illuminate\Support\Facades\Input::class,

Or add this line to the beginning of a controller:

use Illuminate\Support\Facades\Input;

Or write full path when you use it:

(\Illuminate\Support\Facades\Input::has('warranty')) ? true : false;

Solution 4

Add this at top in the controller,

use Illuminate\Support\Facades\Input;
Share:
10,864

Related videos on Youtube

Andrew Vanusi
Author by

Andrew Vanusi

Updated on September 16, 2022

Comments

  • Andrew Vanusi
    Andrew Vanusi over 1 year

    here's my form code :

     <div class="form-group">
              <label>Warranty:</label>
              {{ Form::checkbox('warranty', 0, false) }}
     </div>
    

    here's my controller code :

    public function save(Request $request, $obj = null) {
    
    if (!$obj) {
        $obj = new Service;
    }
    (Input::has('warranty')) ? true : false;
    return $this->saveHandler($request, $obj);
    }
    

    it throws this error Class 'App\Http\Controllers\Input' not found

    any idea ?

  • Andrew Vanusi
    Andrew Vanusi over 7 years
    it throws Class 'Input' not found
  • Andrew Vanusi
    Andrew Vanusi over 7 years
    it throws Class 'Input' not found.
  • Balraj Allam
    Balraj Allam over 7 years
    Do you have Input Class in controllers?
  • Balraj Allam
    Balraj Allam over 7 years
    'App\Http\Controllers\Input ' says you have Input class in controllers which cannot be found. If you want to use Input Facade you have to remove 'App\Http\Controllers\Input ' and replace it with Illuminate\Support\Facades\Input;
  • iDon'tKnoware
    iDon'tKnoware over 2 years
    As far as I know, it was removed time ago: "The Input facade, which was primarily a duplicate of the Request facade, has been removed. If you are using the Input::get method, you should now call the Request::input method. All other calls to the Input facade may simply be updated to use the Request facade." From 6.x documentation: laravel.com/docs/6.x/upgrade#the-input-facade
  • iDon'tKnoware
    iDon'tKnoware over 2 years
    Forgot in previous comment: You need to use $request->input('input_name') instead of Input Facade.