0

I'm trying to save into my database the checkbox I checked into my view. I have a title, a type_article (dropdown), the theme (checkbox) and contents.

I'm doing this in article_list.blade.view :

@foreach ($themes as $theme)  
   <div class="form-check form-check-inline">
     <input class="form-check-input" type="checkbox" name="themeCheckbox[]" value="{{ $theme->theme_id }}">
     <label class="form-check-label">{{ $theme->nom_theme }}</label>
   </div>
@endforeach

My AdminController :

    public function createArticle(Request $request)
    {
       $data = $request->validate([ // $data = $this->validate($request
        'titreArticle' => 'bail|required|between:5,40',
        'typeArticle' => 'bail|required',
        'themeCheckbox' => 'required|array|min:1',
        'themeCheckbox.*' => 'required|string|distinct|exists:theme,nom_theme',
        'contenuArticle' => 'bail|required',
       ]);
       $type_articles = Type_article::findOrFail($data['typeArticle']);
       $article = new Article();
       $article->type_article()->associate($type_articles);
       $themes = Theme::whereIn('nom_theme', $data['themeCheckbox'])->get();
       $article->theme()->associate($themes);
       $article->titre = $data['titreArticle'];
       $article->contenu = $data['contenuArticle'];
       $article->date_creation = date('Y-m-d');
       $article->save();
       $article->theme()->attach($themes);

       return view('admin/panel_admin/panel_admin');
   }

Everything is good except the checkbox issue. I do show my error inside my view and thats what appear when I submit the 3 checkboxs :

The selected themeCheckbox.0 is invalid.
The selected themeCheckbox.1 is invalid.
The selected themeCheckbox.2 is invalid.

I do have a pivot table (Article and Theme as table and Article_theme the pivot table).

EDIT :

The problem was $themes = Theme::whereIn('nom_theme', $data['themeCheckbox'])->get();, I need to change nom_theme to id_theme.

1 Answer 1

1

You should pass $theme->theme_id to the name array of the checkbox. Then, check whether the id exists or not in the array, because default behavior of checkbox is to remove unchecked items.

<input class="form-check-input" type="checkbox" name="themeCheckbox[{{$theme->theme_id}}]" value="1">

I set value of checkbox to 1, it means when you check it, a value of 1 will be assigned to the key of $theme->theme_id. A sample output of this checkbox is something like below:

themeCheckbox=[
    '1' => '1';
    '2' => '1';
    '3' => '1';
    '4' => '1';
    '5' => '1';
];

So, you must look for the keys in your code, not the values as they are unique (1). Modify your controller as below and use array_keys() method in it:

$themes = Theme::whereIn('nom_theme', array_keys($data['themeCheckbox']))->get();

Update

Remove unnecessary validation 'themeCheckbox' => 'required|array|min:1', and fix the other one like below:

'themeCheckbox.*' => 'required',
Sign up to request clarification or add additional context in comments.

8 Comments

Hello, first thank you for the help. I added what you asked me and I have this error below my form : The selected themeCheckbox.$theme->theme_id is invalid. As I said, I've a pivot table with my id_article and theme_id
I forgot to add {{ and }} in name="themeCheckbox[$theme->theme_id]". I've modified it in the answer.
I've this error now The themeCheckbox.1 field has a duplicate value. The themeCheckbox.3 field has a duplicate value. when I check the 1st and 3rd checkbox.
@Leon pay attention to the Update in the answer and fix the validation
Nvm, didn't saw the update. Now i've this error : Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::associate() I guess I have this error since in Article model, I'm doing this : return $this->belongsToMany('App\Models\Theme', 'themes_articles', 'id_article', 'theme_id'); and inside my controller $article->theme()->associate($themes);. I've the belongsToMany relationship and the associate function. With what should I replace associate() ?
|

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.