2

I'm kinda new to Laravel, I ran into some problems with inserting multiple select to multiple columns in the database.

Here my view I'm using select picker for multiple select

  <select multiple="multiple"  name=language[] id="language[]" class="selectpicker"  data-selected-text-format="count">
    <option name="En">English</option>
    <option  name="It">Italian</option>
    <option  name="Ar">Arabic</option>                            
    <option  name="Jp">Japanese</option>
    </select>

The languages table:

+----+---------+---------+--------+--------+
| id | English | Italian | Arabic |Japanese|
+----+---------+---------+--------+--------+
|  1 |         |         |        |        |
|  2 |         |         |        |        |
|  3 |         |         |        |        |
+----+---------+---------+--------+--------+

How can I insert data to each column in the controller?
Here what I'm trying to do!

+----+---------+---------+--------+--------+
| id | English | Italian | Arabic |Japanese|
+----+---------+---------+--------+--------+
|  1 |   En    |         |   Ar   |   Jp   |
|  2 |         |    It   |        |        |
|  3 |   En    |         |        |   Jp   |
+----+---------+---------+--------+--------+

2 Answers 2

1

First, make sure the tables id column is set to auto increment. and other columns as nullable.

$languages = request('languages');

DB::table('table_name')->insert([
    'English' => in_array('En', $languages) ? 'En' : null,
    'Italian' => in_array('It', $languages) ? 'It' : null,
    'Arabic' => in_array('Ar', $languages) ? 'Ar' : null,
    'Japanese' => in_array('Jp', $languages) ? 'Jp' : null,
]);
Sign up to request clarification or add additional context in comments.

Comments

0

You'll receive an array language in your controller so you can check if the language exists add it to the DB :

$en = $it = $ar = $jp = null;

if(in_array('En', $language))
    $en = 'En';
if(in_array('It', $language))
    $it = 'It';
if(in_array('Ar', $language))
    $ar = 'Ar';
if(in_array('Jp', $language))
    $jp = 'Jp';

ModelName::insert(['English' => $en, 'Italian' => $it, 'Arabic' => $ar, 'Japanes' => $jp]);

2 Comments

thank you for answering i try it and that gave me null inserting for all of them.
You must debug your code and give me the right specific issue. or you can share with us the content of your controller or just the relevant action code.

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.