dimanche 11 octobre 2020

Getting this error (Call to a member function extension() on null) when uploading image to public/images

I'm getting this error "Call to a member function extension() on null" when having a store procedure to database with images. i want to save the file name of the image to database and the file will store to the public/images folder.

Here's my controller code

public function createPost(Request $request){

        $title = $request->title;
        $description = $request->description;
        $image=$request->file('file');
        $imageName = time().'.'.$image->extension();
        $image->move(public_path('images'),$imageName);
        
        $post = new Post();
        $post->title=$title;
        $post->description=$description;
        $post->imageblog=$imageName;
        $post->save();
        return redirect('/posts')->with('post_created', 'Post has been created successfully');
    }

The View File

<div class="container">
        <form method="POST" action="" enctpye="multipart/form-data">
            @csrf
            <div class="row">
                    <div class="col-25">
                        <label for="fname">Choose Image</label>
                    </div>
                    <div class="col-75">
                        <input type="file" name="file" onchange="preview_image(event)" />
                        <img id="output_image" alt="Blog Image">
                    </div>
                </div>
                <div class="row">
                    <div class="col-25">
                        <label for="fname">Post Title</label>
                    </div>
                    <div class="col-75">
                        <input type="text" name="title" placeholder="Enter Post Title">
                    </div>
                </div>
                <div class="row">
                    <div class="col-25">
                        <label for="Desription">Desription</label>
                    </div>
                        <div class="col-75">
                             <textarea id="subject" name="description" style="height:200px"></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-85">
                            <input type="submit" value="Submit">
                        </div>
                    </div>
            </form>
        </div>



Aucun commentaire:

Enregistrer un commentaire