mozew's avatar
Level 6

How to show default value in select option (parent_id)?

I tried for save a parent_id of Category in laravel 5.8 successfully. but I want to edit category now.

CategoryController.php

public function edit(Category $category)
{
    return view('Admin.categories.edit', compact('category'));
}

public function update(CategoryRequest $request, Category $category)
{
    $category->update($request->all());
    return redirect(route('categories.index'));
}

edit.blade.php

        <form action="{{ route('categories.update', $category->id) }}" method="post">
            {{ method_field('PATCH') }}
            {{ csrf_field() }}
            @include('Admin.layouts.errors')
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" class="form-control" value="{{ old('name') ? : $category->name  }}" id="name" name="name">
            </div>
            <div class="form-group">
                <label for="parent_id">Sub Category</label>
                <select class="form-control" id="parent_id" name="parent_id" data-live-search="true">
                    @foreach(\App\Category::all() as $category)
                        <option value="{{ $category->id }}" {{ trim($category->id) , $category->pluck('id')->toArray() ? 'selected' : ''  }}>{{ $category->name }}</option>
                    @endforeach
                </select>
            </div>
            <button type="submit" class="btn btn-primary">Save</button>
        </form>
0 likes
9 replies
Cronix's avatar

Are your other issues fixed yet? Please mark them as solved if so. You keep adding new problems and it's hard to know if the old ones we've been spending our time helping you with are fixed or not before continuing on with new problems.

Cronix's avatar
@foreach(\App\Category::all() as $category)
    <option value="{{ $category->id }}" {{ trim($category->id) , $category->pluck('id')->toArray() ? 'selected' : ''  }}>{{ $category->name }}</option>
@endforeach

For one thing, this doesn't make sense at all, specifically this part

{{ trim($category->id) , $category->pluck('id')->toArray() ? 'selected' : ''  }}

I don't know how you're going to select options, unless you somehow pass those options (or the ids's of the options) that are supposed to be selected, to the view. You're looping all options available.

How do you know what options are supposed to be selected? I don't see any code that would tell you that.

mozew's avatar
Level 6

@CRONIX - I do not want to select option. I want to select option a value that selected database.

Cronix's avatar

I want to select option a value that selected database.

Can you please reword that? I don't understand what you're meaning, because to me it looks like you are clearly trying to mark an option as selected. It shows that right in the code, right here:

{{ trim($category->id) , $category->pluck('id')->toArray() ? 'selected' : ''  }}

If all you are trying to do is show all categories, and not select any of them via code, why are you trying to add a selected attribute instead of just outputting them?

@foreach(\App\Category::all() as $category)
    <option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
1 like
jlrdw's avatar
jlrdw
Best Answer
Level 75

I think OP means what is currently stored in the DB. (I think)

See https://laracasts.com/discuss/channels/laravel/how-do-i-assign-the-existing-value-to-a-select

By retrieving what's stored just like inputs.

@irankhosravi I have already mentioned that most questionS like this has been answered many times, go to google and type:

site:laracasts.com YOUR SEARCH EXPRESSION   

Takes just a few seconds to find many past wonderful replies.

1 like
mozew's avatar
Level 6

For example

I have categories in database

id          name            parent_id

1       Software        0
2       Hardware        0
3       Photoshop       1
4       CoredDraw       1

Now in the if a user for example select CorelDraw for edit

open edit for it.

Select the name input tag write CorelDraw

Select the parent_id option tag select Software. Because this is parent_id is = 0

jlrdw's avatar

Did you even bother to look at the link I gave. It talks about your question.

Please or to participate in this conversation.