ninkovic90
ninkovic90

Reputation: 45

Displaying a category when editing a post in laravel

I am creating a blog in Laravel. I have two tables - posts and categories. When i want to edit one post, i have a problem with categories. My post controller is sending categories to the view, i can see them while editing, but i don't know how to set the current category in the select option. The tables are conected:

public function posts(){

    return $this->hasMany('App\Post');
}

public function category(){

    return $this->belongsTo('App\Category');
}

Controller:

public function edit(Post $post)
{
    $categories = Category::all();

    return view('admin.posts.edit', compact('post'))->withCategories($categories);
}

View:

<form action="{{ route('posts.update', ["post" => $post->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
            @csrf
            <div class="form-body">
                <div class="form-group">
                    <label class="col-md-3 control-label">Naslov</label>
                    <div class="col-md-4">
                        <input type="text" name='title' value='{{ old("title", $post->title) }}' class="form-control">
                        @if($errors->has('title'))
                            <div class='text text-danger'>
                                {{ $errors->first('title') }}
                            </div>
                        @endif
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Kategorija</label>
                    <div class="col-md-4">
                        <select class="form-control" name="category_id">
                            @if(count($categories) > 0)
                                @foreach($categories as $category)
                                    <option value="{{ $category->id }}">{{ $category->name }}</option>
                                @endforeach
                            @endif
                        </select>
                        @if($errors->has('category_id'))
                            <div class='text text-danger'>
                                {{ $errors->first('category_id') }}
                            </div>
                        @endif
                    </div>
                </div>

How can i make the category display the current category of the selected post in the option select like i have for title? value='{{ old("title", $post->title) }}'

Upvotes: 2

Views: 2156

Answers (2)

Thomas Van der Veen
Thomas Van der Veen

Reputation: 3226

What you are looking for is the selected property of the <option> element.

@forelse ($categories as $category)
    <option value="{{ $category->id }}" {{ $category->id == old('category_id', $post->category_id) ? 'selected' : '' }}>
        {{ $category->name }}
    </option>
@endforelse

A minor change as well in the in you blade template to make use of the @forelse directive for less code.

You could also change your controller code a bit:

return view('admin.posts.edit', compact('post', 'categories'));

Further the laravelcollective/html package might help you a lot in creating forms.

Upvotes: 0

Tim Lewis
Tim Lewis

Reputation: 29257

To show the currently associated Category, you can set a default "selected" option based on the $post->category_id:

<option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>

This a simple ternary that sets the selected attribute of one of the options based on the old() value, or, if old() is not set, then based on the value of $post->category_id

Upvotes: 3

Related Questions