Meysam Saberi Meysam Saberi - 1 year ago 277
PHP Question

checkbox to mysql in laravel 5.2

how to save checkbox value to database ?

my view code:

<input type="checkbox" name="checkbox" id={{$cat->id}} value={{$cat->id}}>

my controller code:

public function submitArticle(){
$article = new Post();
$article->title= Input::get('title');
$article->body= Input::get('body');
$article->cat = Input::get('checkbox');
$articleId = $article->id;
return $articleId;

Answer Source

use this code

<input type="checkbox" name="checkbox[]" id={{$cat->id}} value={{$cat->id}}>

and in controller

        $relationCategory = $checkbox;
        foreach ($relationCategory as $relCat){
            $rel = new Relationship();
            $rel->post_id = $articleId;
            $rel->cat_id = $relCat;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download