0

So far, I know I need to store the checkbox values in an array like so:

<input name="thinking_traps[]" type="checkbox" value="1">
<input name="thinking_traps[]" type="checkbox" value="2">
<input name="thinking_traps[]" type="checkbox" value="3">

But I'm not sure how to correctly pass these values to the controller and add them to the migration table? Will each value be added as a separate column in the database?

Here I've tried adding adding them like any other input:

public function store(Request $request)
    {
        $this->validate($request, [
            'thought_entry' => 'required'
        ]);



        $entry = new ThoughtJournal;
        $entry->user_id = auth()->user()->id;
        $entry['entry_date'] = date('Y-m-d H:i');
        $entry->thought_entry = $request->input('thought_entry');
        $entry->emotions = $request->input('emotions');
        $entry->thinking_traps = $request->input('thinking_traps');
        $entry->balanced_thought = $request->input('balanced_thought')
        $entry->save();

        return redirect('/dashboard');
    }
public function up()
    {
        Schema::create('thoughtjournal', function (Blueprint $table) {
            $table->increments('entry_id');
            $table->integer('user_id');
            $table->date('entry_date');
            $table->mediumText('thought_entry');
            $table->
            $table->timestamps();
        });
    }
8
  • How are you expecting to store these 3 occurances into the db table, please dont say a comma delimited list Commented Feb 11, 2020 at 15:57
  • Also, if a check box is NOT checked, it is not sent to the PHP, so this thinking_traps array could be 0,1,2 or 3 occurances. You are not even treating it like an array yet Commented Feb 11, 2020 at 15:59
  • That's what I'm asking, how would they be added to the migration table and thus stored in the database? Commented Feb 11, 2020 at 16:00
  • Well you are designing the database, how do you want to store place these values on your database Commented Feb 11, 2020 at 16:01
  • I've just used them 3 occurrences as an example. I have many checkboxes but ideally I want the user to only be able to select up to 3 so there will be unchecked boxes as well Commented Feb 11, 2020 at 16:02

3 Answers 3

1

I don't see that thinking_traps in your table migration file

Model

class ThoughtJournal{

   // Add it's type to casts as array
   public $casts = ['thinking_traps'=> 'array'];

   // Add it to fillables if you haven't
   public $fillable = ['thinking_traps',...];

   //...
}

Storing

Second, in your storing method, when no item is selected in checkbox you need to set it as array, like so

   ...
   $entry->thinking_traps     = $request->has('thinking_traps') 
                              ? $request->get('thinking_traps') 
                              : [];

And as a precaution, for the validation you can have it like so:

$this->validate($request, [
   'thinking_traps' => 'nullable|in:1,2,3' // your values
]);

Migrations

And for the migration i don't see that you have the column set there, you can modify your migration file like so:

// You can have it as json
$table->json('thinking_traps')->nullable();

// or string
$table->string('thinking_traps')->nullable();

Or you can create a separate migration file if you don't want to recreate the table in shell/cmd through artisan like so:

php artisan make:migration add_thinking_traps_to_thoughtjournal_table --table=thoughtjournal

This command will add a migration file, separately, and in there add that column like how you normally would, you can then php artisan migrate to add that column.

Let us know if that works.

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

Comments

0

You should not store multiple values in a single row in your database. You should store it separately so your data are normalized.

Comments

0

Post values will be an array even if you select one checkbox.

$thinking_traps = $request->input('thinking_traps');

$thinking_traps will be an array So try to save array values in separate table as multiple rows with foreign key

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.