php - laravel5: Storing of checkboxes as tinyint(4) or boolean in mysql/DB -


looking better execution of below function, dealing checkboxes.

    /**  * update specified resource in storage.  *  * @param  int  $id  * @return response  */ public function update(createuserrequest $request, $id) {             $user = user::find($id);     //todo should else where?     $input = $request->all();     if(isset($input['status'])) $input['status'] = 1;         else $input['status'] = 0;     $user->fill($input)->save();             return redirect('admin/users');       } 

i think doing fine, can test value of status string 'yes'. unchecked checkboxes don't register anything, you're going doing same thing. assuming checkbox input has value="yes", then:

$input = $request->all(); if($input['status'] === 'yes') $input['status'] = 1; else $input['status'] = 0; 

pretty no matter what, you're going have have 'else' define value unchecked case. it's matter of want use checked value. make 1, unchecked case wouldn't 0, you'd still need condition assign it.

if you're wanting logic out of controller, might have tried setting field default field 0 in ddl, in model implementing mutator (setter):

class user extends eloquent {      public function setstatusattribute($value)     {         $this->attributes['status'] = 1;     }  } 

the trouble here won't called if don't have status in input , won't have if checkbox unchecked. won't unset if user sets , unsets.

hence, best of knowledge, generate both true , false values input in controller conventional practice. you're real choice value prefer set on input in form.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -