nafeeur10's avatar

Show data during edit in Laravel

<input type="text" value="{{ old('category_name', isset($category) ? $category->category_name : '') }}">

I am using same logic input value="{{ old('category_name', isset($category) ? $category->category_name : '') }}" so that it can be same for Create and Edit.

But what should the logic for Image and Checkbox?


<input type="checkbox" value="{{ old('category_is_menu', isset($category) ? 'checked' : '') }}"/>


<input class="col-sm-12 form-control-file" id="category_picture" type="file" name="category_picture" />
       @if(isset($category))
            <img src="{{ asset('public/images/category/'.$category->category_picture) }}" alt="Category Picture">
       @endif
   <div id="category-image-holder"></div>
</div>

For create When I will upload image it will show in category-image-holder but when I am in Edit mode, it should be hide and show when image will be updated.....

0 likes
4 replies
a4ashraf's avatar

@nafeeur10

Try this for check box

<input type="checkbox" name="filed_name[]" value="1" {{ (is_array(old('filed_name')) and in_array(1, old('filed_name'))) ? ' checked' : '' }}>

The file input can't be prepopulated by Laravel or by any software. actually your website doesn't and shouldn't know the local path to the file. so you need to keep it by session

Snapey's avatar

when creating, pass a new model to the view so that all the fields are present but empty, or their default values.

For instance, I set my crud controllers like this (using route model binding)


    public function create()
    {
        $template = new Template();

        return $this->edit($template);
    }

    public function edit(Template $template)
    {
        return view('template.edit', compact('template'));
    }

so create function passes through edit, and I only have an edit view not a create view

you can then do this

 <input type="text" value="{{ old('category_name', $category->category_name) }}">

no need for checks. This will make dealing with your other input types easier.

1 like
nafeeur10's avatar

<input type="checkbox" value="{{ old('category_is_menu', isset($category->category_is_menu) ? 'checked' : '') }}"/>

I want to know why it is not working!!!

I am retrieving checkbox value from database.

Checking with isset()

But not working!!!!

a4ashraf's avatar

@nafeeur10

you are not adding checkbox name

try this

<input type="checkbox" name="category_is_menu" value="{{ old('category_is_menu', isset($category->category_is_menu) ? 'checked' : '') }}"/>

Please or to participate in this conversation.