1

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');
    $article->save();
    $articleId = $article->id;
    return $articleId;
}
4
  • 1
    What have you tried? Should be the same as any other data you insert. Is there a particular issue you have with this field? Commented Apr 28, 2016 at 22:13
  • yes,my problem with checkbox @Goose Commented Apr 29, 2016 at 6:23
  • 1
    I don't understand. I know you have a problem with checkbox, but what have you tried? What behavior do you get with checkbox that you don't get with other inputs? Commented Apr 29, 2016 at 13:08
  • edited question @Goose Commented Apr 30, 2016 at 6:06

1 Answer 1

2

use this code

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

and in controller

if(is_array($checkbox)){
        $relationCategory = $checkbox;
        foreach ($relationCategory as $relCat){
            $rel = new Relationship();
            $rel->post_id = $articleId;
            $rel->cat_id = $relCat;
            $rel->save();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.