Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How can I set the default value Laravel <select> element?

I want to retrieve database values in category name and i want to show default value in selection. This is my controller for my edit view.

I have a category_product table.

category_product

<div class="col-md-4">
    <div class="form-group">
        <label for="category">category</label>
        <select class="form-control" name="category" id="category">
            @foreach($categories as $category)
                <option value="{{ $category->id }}" {{ $product->categories()->category_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
            @endforeach
        </select>
    </div>
</div>

ProductController.php

public function edit(Product $product)
{
    $categories = Category::all();
    return view('Admin.products.edit', compact('product', 'categories'));
}

Product.php

public function categories()
{
    return $this->belongsToMany(Category::class);
}

I get this error.

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id

0 likes
6 replies
Nakov's avatar

@oxbir try this

<option value="{{ $category->id }}" {{ $product->category_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
3 likes
shahr's avatar
Level 10

Still selected the first option

shahr's avatar
Level 10

I change this

  {{ dd($product->category_id) }}

it display null

Nakov's avatar

that means that you either don't have category for that product or your relationship is not correctly setup..

Do you have category_id in the products table?

mstrauss's avatar
mstrauss
Best Answer
Level 14

@oxbir

Since the Product can have many Categories per your relationship and the pivot table implication, you would have to use an in_array method (or something similar) like:

            @foreach($categories as $category)
                <option value="{{ $category->id }}" {{ (in_array($category->id, $product->categories->pluck('id')->toArray()) ? 'selected' : '' }}>{{ $category->name }}</option>
            @endforeach

Does each Product possibly belong to many different Categories? Or does each Product belong to one Category? Because if the later is true, then you would have to adjust your relationship to a oneToMany as opposed to the current manyToMany with the pivot table.

2 likes
Geeb's avatar

I have a similar issue. I am trying to set the default value of user element to be the user currently signed in. @include ('partials.forms.edit.user-select', ['translated_name' => trans('general.user'), 'fieldname' => 'assigned_user', 'required'=>'true'])

The partials.forms.edit.user-select.php file is as follows:

{{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }}

<div class="col-md-6{{  ((isset($required)) && ($required=='true')) ? ' required' : '' }}">
    <select class="js-data-ajax" data-endpoint="users" data-placeholder="{{ trans('general.select_user') }}" name="{{ $fieldname }}" style="width: 100%" id="assigned_user_select" aria-label="{{ $fieldname }}">
        @if ($user_id = old($fieldname, (isset($item)) ? $item->{$fieldname} : ''))
            <option value="{{ $user_id }}" selected="selected" role="option" aria-selected="true"  role="option">
                {{ (\App\Models\User::find($user_id)) ? \App\Models\User::find($user_id)->present()->fullName : '' }}
            </option>
        @else
            <option value=""  role="option">{{ trans('general.select_user') }}</option>
        @endif
    </select>
</div>

<div class="col-md-1 col-sm-1 text-left">
    @can('create', \App\Models\User::class)
        @if ((!isset($hide_new)) || ($hide_new!='true'))
            <a href='{{ route('modal.show', 'user') }}' data-toggle="modal"  data-target="#createModal" data-select='assigned_user_select' class="btn btn-sm btn-primary">{{ trans('button.new') }}</a>
        @endif
    @endcan
</div>

{!! $errors->first($fieldname, '<div class="col-md-8 col-md-offset-3"><span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span></div>') !!}

Any help would be greatly appreciated.

Please or to participate in this conversation.