-1

I'm new to laravel. I'm trying to insert multiple select values from listbox to a database table. This is the HTML of the select drop down I'm using:

<select name="tsubject[]" id="tsubject[]" multiple>
    <option value="0">select</option>
    <option value="English">English</option>
    <option value="Malayalam">Malayalam</option>
    <option value="Hindi">Hindi</option 
    <option value="Maths">Maths</option>
</select>

My controller.php is

else{
    $teachers =new ForumTeacher(); 
    $teachers ->tsubject=Input::get('tsubject[]');
}

The error that occurs is as follows;

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tsubject' cannot be null (SQL: insert into `forum_teachers` (`tname`, `tmobile`, `ttype`, `tcls`, `tdivn`, `tsubject`, `updated_at`, `created_at`) values (NamithaP, 9876543211, normalteacher, 6, D, , 2015-02-12 09:09:16, 2015-02-12 09:09:16)) 
4
  • 1
    Please edit the question rather than posting an unreadable wall of code in comments. Commented Feb 12, 2015 at 8:45
  • 1
    Can you tell us more about your problem, with that poor informations, we can do more than asking questions... Commented Feb 12, 2015 at 8:54
  • srry...im trying to inset multiple values into database single row...Ihave a select box that contain 4 subjects.im select 3 subject using this select code <select name="tsubject[]" id="tsubject[]" multiple><option value="English">English</option> <option value="Malayalam">Malayalam</option> <option value="Hindi">Hindi</option <option value="Maths">Maths</option> </select> but i can not save into database(i want in database like this eg:english,hindi,maths).i dont know how to write code in controler.php page using multiple selete? Commented Feb 12, 2015 at 9:12
  • 1
    This is a great question. Lots of examples on the Internet about how to insert a variable that is a string or num into a RDBM - it's simple - but basically no clear, good ones when that variable is an array as the result of a multi select form input. Commented Jul 18, 2015 at 20:46

2 Answers 2

1

I assume that you have a teacher table and subject table and you have many-to-many relationship and is linked in a pivot table 'subject_teacher'

In laravel it is easier to use of blade.

First in your controller you have to populate your subjects list

$subjectsList= Subjects::lists('subjecName','id');
//    $previousSelectedSubjects = some optional selected subjects to
return View::make('yourview')->with('subjectsList', $subjectsList)->with('previousSelectedSubjects ', $previousSelectedSubjects);

Then in your view use it like this:

    {{ Form::select('subjects[]', $subjectsList, $previousSelectedSubjects->lists('id'), array('multiple'=>'multiple'))}}

and in your controller, after posting data, use submitted data

 if (Input::has('subjects'))
    foreach(Input::get('subjects')as $subject)
    {
        $forumTeacher= new ForumTeacher; // I think this is your intermediat, pivot, table
        $forumTeacher->teacher_id= $id; // you should know how to find this id
        $forumTeacher->subject_id= $subject->id;        
        $forumTeacher->save();  
    }

If some thing is not clear, don't hesitate to ask.

Sign up to request clarification or add additional context in comments.

6 Comments

i have only one table tbl_subject , and i get the subject from this table also checkall function is done...im only want save(insert )this checked values into tbl_catrgory table in database. i think its similar function of selectall values insert into database.
Plz, reformulate your question again. you mentioned else{ $teachers =new ForumTeacher(); $teachers ->tsubject=Input::get('tsubject[]'); } and now you say category. I can't understand your situation.
oh srry....else{ $teachers =new ForumTeacher(); $teachers ->tsubject=Input::get('tsubject[]'); } this for select all from listbox insert into db.. now i want checkall function...im checked 5 subject using checkall and how to save (insert) this 5 subjects into database table same field like this(eg:english,math,hindi,..)how to insert into it what is controller.php code....
Unfortunately, I can't get your problem. I recommend you to forget about the code you wrote. Explain in pain English what you need exactly.
srry............... i have 5 subjects .im fetched from database (table:subject) to display webpage.and im create 5 checkbox to each subjects .im create 2 submit buttons ..1.checkall button and 2. submit button. then im click checkall button all checkboxes are checked(tik mark).then i want im click submit button these 5 subjects are insert to database (table:mark). like this (eg:english,math,hindi,..) what is the code in controller.php... sorry for my bad english..
|
0

As you are wanting to get the input use $subjects = Input::get('tsubject'); forget about the [].

If you want them all in the same field, do the following;

$teachers =new ForumTeacher(); 
$teachers->tsubject = implode(',', Input::get('tsubject'));
$teachers->save();

Optionally, once you have the input you can do the following to insert for each of the subjects like so;

foreach ($subjects as $subject){
    $teachers = new ForumTeacher(); 
    $teachers->tsubject = $subject;
    $teachers->save();
}

This will create and save your data to your table.

What I Recommend

By the looks of the query you are doing, it is not best practice to store multiple options in a single field. I think it would be best you set up a pivot table to handle the relationships between the teacher and subjects.

For example;

Teacher Table Data (without the tsubject field):
id, tname, tmobile, ttype, tcls, tdivn, updated_at, created_at
1, NamithaP, 9876543211, normalteacher, 6 , D, 2015-02-12 09:09:16, 2015-02-12 09:09:16

Subject pivot Table data:
id of NamithaP, id of Subject

Subject table:
Subject Id, Maths
Subject Id, English
....

You can read more about many to many relations ships here.

4 Comments

thank you ..............$teachers =new ForumTeacher(); $teachers->tsubject = implode(',', Input::get('tsubject')); this code working..
is it similar to checkall values insert into database? im trying to checkall function eg:5 subject and corresponding checkbox.then 2 buttons 1.checkall ,2.submit... click a 1st button checkall checked all subjects..then clck submit button. i want allchecked values go to database....what to do????
Answer for that question found here
@chips if you have found the answer to your question please mark as an answer for others. I also recommend if you are storing multiple options in one field you normalise your database tables to use pivots. This is in the What i recommend section of the answer.

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.