0

I already imploded all my checkboxes into 1 value from spoken_language[] including 1 textfield value.

html

<div>
    {{ Form::checkbox('spoken_language[]', "English", array('class' => 'form-check-input')) }}
    <label>English</label>
</div>
<div>
    {{ Form::checkbox('spoken_language[]', "Bahasa Melayu", array('class' => 'form-check-input')) }}
    <label>Bahasa Melayu</label>
</div>
<div>
    {{ Form::checkbox('spoken_language[]', null, array('class' => 'form-check-input')) }}
    <label>Others</label>
    {{ Form::text('spoken_language[]', null, array('class' => 'form-control')) }}
</div>

php

$request->merge(['spoken_language' => implode(',', (array) $request->get('spoken_language'))]);

The problem is how I can do the data binding into blade template view (checkbox and textfield) based on 1 value?

My collection

<?php

$user = DB::table('users')
    ->where("users.id", Auth::user()->id)
    ->first();

    return View::make('user.profile')->with('user', $user);

Like this example, I got these value English,Bahasa Melayu,Urdu, then I need to assign for each checkbox and Urdu in textfield. But if English,Bahasa Melayu, just checkbox only. Same goes to Urdu, goes to textfield.

1 Answer 1

1

Say $storedSpokenLang has the values retrieved in the database.

You can create a collection of user's saved languages and pass that to the blade view template.

$spokenLang = collect(explode(',', $storedSpokenLang))->flip();

In the view, you can start from here:

<div>
    {{ Form::checkbox(
           'spoken_language[]', 
           "English", 
           $spokenLang->has('English'), 
           array('class' => 'form-check-input')) }}
    <label>English</label>
</div>
<div>
    {{ Form::checkbox(
           'spoken_language[]',
           "Bahasa Melayu", 
           $spokenLang->has('Bahasa Melayu'),
           array('class' => 'form-check-input')) }}
    <label>Bahasa Melayu</label>
</div>
<div>
    {{ Form::checkbox(
           'spoken_language[]', 
           $spokenLang->except("English", "Bahasa Melayu", "")->isNotEmpty(),
           array('class' => 'form-check-input')) }}
    <label>Others</label>
    {{ Form::text(
           'spoken_language[]', 
           $spokenLang->except("English", "Bahasa Melayu", "")->keys()->implode(','),
           array('class' => 'form-control')) }}
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

I already updated my question with the collection. How to bind $spokenLang into view from there?
@MohammadNurdin in your View::make()->with()->with('spokenLang', $spokenLang);

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.