1
1
1programmer2018-07-20 12:26:30
Laravel
1programmer, 2018-07-20 12:26:30

How to display the index of a tag, not its name?

Welcome all. I am familiar with laravel and relationships.
There is html, a list of categories is displayed here. Typical CRUD application.

<select class="form-control select2" multiple="" data-placeholder="Select a State"
                                            style="width: 100%;" name="categories[]" tabindex="-1" aria-hidden="true">
                                        @foreach($categorys as $category)
                                            <option value="{{ $category->id }}">{{ $category->name }}</option>
                                        @endforeach
                                    </select>

Next controller to write to DB
$post = post::find($id);
        $post->fill($req->all());
        $post->categories()->sync($req->categories);
        $post->save();

Question:
Why if I check the answer $req->all() it outputs "categories": ["Ghbdtn"] and not "categories": ["1"] ? Because of this, do not write to the DB.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-07-20
@Yan-s

Check what you have in the value of the select. The array must contain values ​​from this attribute.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question