dpakagarwal's avatar

image key not showing in dd request in laravel 8

while editing the form it shows me an image preview, but after submitting using the update button it shows all fields except image in dd($request->all()). See below code:

edit.blade.php

<form class="row g-3" action="{{ route('admin.Blog.update', $post->id) }}"
                            method="POST" enctype="multipart/form-data">
                            @csrf
                            @method('PATCH')
                            <div class="col-6">
                                <label class="form-label">Title</label>
                                <input type="text" name="title" class="form-control" placeholder="Post Title"
                                    value="{{ $post->title }}">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label mb-2">Image</label>
                                <input class="form-control" type="file" id="f_image" name="image">
                                <img src="/images/Post/{{ $post->image }}" width="300px">
                                @error('image')
                                    <div class="text-danger">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="col-6">
                                <label class="form-label">Tags</label>
                                <select class="multiple-select" name="tags[]" data-placeholder="Choose Tags"
                                    multiple="multiple">
                                    @foreach ($tags as $tag)
                                        <option value="{{ $tag->id }}" @foreach ($post->tags as $postTag)
                                            @if ($postTag->id == $tag->id)
                                                selected
                                            @endif
                                    @endforeach
                                    >{{ $tag->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-6">
                                <label class="form-label">Categories</label>
                                <select class="multiple-select" name="categories[]" data-placeholder="Choose Categories"
                                    multiple="multiple">
                                    @foreach ($categories as $category)
                                        <option value="{{ $category->id }}" @foreach ($post->categories as $postCategory)
                                            @if ($postCategory->id == $category->id)
                                                selected
                                            @endif
                                    @endforeach
                                    >{{ $category->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-12">
                                <label class="form-label">Description</label>
                                <textarea id="editor" name="body" class="form-control" placeholder="Description"
                                    rows="4" cols="4">{{ $post->body }}</textarea>
                            </div>
                            <div class="col-6">
                                <div class="form-check mt-4">
                                    <input class="form-check-input" name="status" type="checkbox" id="flexCheckDefault"
                                        value="1" @if ($post->status == 1) checked @endif>
                                    <label class="form-check-label" for="flexCheckDefault">Publish</label>
                                </div>
                            </div>
                            <div class="col-12">
                                <button class="btn btn-primary px-4">Update</button>
                                <a href="{{ route('admin.Blog.index') }}" class="btn btn-secondary px-4">Cancel</a>
                            </div>
                        </form>

Postcontroller.php

public function update(Request $request, Post $id)
    {
        dd($request->all());
        $request->validate([
            'title' => 'required',
            'subtitle' => 'required',
            'body' => 'required'
        ]);
        $input = $request->all();

        if ($image = $request->file('image')) {
            $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move(public_path() . '/images/Post/', $profileImage);
            $input['image'] = $profileImage;
        } else {
            unset($input['image']);
        }

        $post = Post::find($id);
        $post->title = $input['title'];
        $post->image = $input['image'];
        $post->subtitle = $input['subtitle'];
        $post->slug = Str::slug($input['title'], '-');
        $post->body = $input['body'];
        $post->status = $input['status'];
        $post->tags()->sync($input['tags']);
        $post->categories()->sync($input['categories']);
        $post->save();
        return redirect(route('admin.Blog.index'))->with('success', 'Post updated successfully.');
    }

and showing me error as :

Undefined array key 'image'
0 likes
3 replies
geowrgetudor's avatar

$request->all() is returning just the input data. You won't have files there.

Snapey's avatar
Snapey
Best Answer
Level 122

change this

unset($input['image']);

to

$input['image'] =null;
meric's avatar

maybe you havent put utf-8 in your (parent) html code? I had same issue, spent hours looking for solution. I

Please or to participate in this conversation.