3

This is my view for the customer register.

<form action="store" method="post">

    <input type="hidden" name="_token" value="{{csrf_token()}}">

    <label for="name">Name</label>
    <input type="text" name="name">
    <br>

    <label for="email">Email</label>
    <input type="text" name="email">
    <br>

    <label for="country">Country</label>
    <select name="country" id="country">
        <option value="india">India</option>
        <option value="srilanka">SriLanka</option>
        <option value="usa">USA</option>
    </select>
    <br>

    <input type="radio" name="gender" value="male">
    <label for="male">Male</label>

    <input type="radio" name="gender" value="female">
    <label for="female">Female</label>
    <br>

    <input type="checkbox" name="favorite[]" id="south" value="south">
    <label for="south">South</label>

    <input type="checkbox" name="favorite[]" id="north" value="north">
    <label for="north">North</label>

    <input type="checkbox" name="favorite[]" id="east" value="east">
    <label for="east">East</label>

    <br>

    <label for=""></label>
    <input type="submit" name="submit" value="Submit">

</form>

Well all values are going to database but the checkbox are going as array.

But if i remove [] in favorite. The last checkbox value is going to database.

This is my controller code

public function store(Request $request)
{
    $user= laravel::create(Request::all());
    return "data saved";
}

And this is my model

class laravel extends Model
{
    protected $fillable = [
        'name', 
        "email", 
        "gender", 
        "country", 
        "favorite"
    ];
}

Can any one tell me how to send all checkbox values to database whatever customer is checked.

I want to know how to edit checkbox to update.

Thank you in advance.

1
  • What type of field is favourite in your database? What does the schema look like? And do you intend to store multiple favorites? Commented Aug 8, 2016 at 6:20

1 Answer 1

4

Make a text column in your table with the name of favorite and use this logic to store your values inside of it as csv

public function store(Request $request)
{
    $request->merge([ 
        'favorite' => implode(',', (array) $request->get('favorite'))
    ]);

    laravel::create($request->all());

    return "data saved";
}
Sign up to request clarification or add additional context in comments.

9 Comments

Nice. Like the use of ->replace
@zayn Ali. I updated my code with your code it is showing me. Call to undefined method Illuminate\Support\Facades\Request::replace()
use the $request variable which is passed in your controller method. like in used in my answer
@Zayn Ali. I am geting same error will please check my name space namespace App\Http\Controllers; use Illuminate\Http\request; use App\Http\Requests; use App\laravel; use Illuminate\Support\Facades\Input;
it works for me to insert multiple checkbox values to database: $comp_ids = implode(' ,',(array)$request->get('comp_ids'));
|

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.