Error : Call to a member function getClientOriginalExtension() on string laravel 9

94 views Asked by At

im trying to upload image in laravel but have this error: Call to a member function getClientOriginalExtension() on string

this the controller code where i add post to database with the image name:

public function create()
    {

        return view('frontoffice.posts.create');
    }
    
    // Store a newly created post in the database
    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'content' => 'required',
            'category' => 'required|string',
            
        ]); 
        $file_extensions = $request-> photo -> getClientOriginalExtensions();
        $file_name= time(). '.' .$file_extensions;
        $path = 'images/posts';
        $request -> photo -> move($path,$file_name);
        $post = new Post;
        $post->title = $request->title;
        $post->user_id = "1";
        $post->content = $request->content;
        $post->category = $request->category;
        $post->likes =  0;
        $post-> photo =    $file_name;
        $post->save();

       

        return redirect()->route('posts.index')->with('success', 'Job created successfully');
    }

the blade code where i added the enctype="multipart/form-data" to the form :

  <form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
            
                    
                <div class="col-lg-12">
                    <div class="form-group">
                     
                         <input class="form-control" name="photo" type="file" id="photo"> 
                    </div>
                </div>
                <div class="col-lg-12">
                    <ul class="d-flex  justify-content-center ">
                        <li><button class="active" type="submit" value="post">Post</button></li>
                        <li><a href="{{ route('posts.index') }}" title="" >back</a></li>
                    </ul>
                </div>
            </div>
        </form>

filesystems:

'posts' => [
            'driver' => 'local',
            'root' => base_path() . 'public/images/posts/',
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

 
1

There are 1 answers

0
Koussay On

Although the error you described isn't related to this one, but you need to add csrf protection to your form, since you're using post method, because otherwise, the form won't work (you will have 419 page expired error). So your Blade will be like this

    <form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="col-lg-12">
            <div class="form-group">

                <input class="form-control" name="photo" type="file" id="photo">
            </div>
        </div>
        <div class="col-lg-12">
            <ul class="d-flex justify-content-center">
                <li><button class="active" type="submit" value="post">Post</button></li>
                <li><a href="{{ route('posts.index') }}" title="">back</a></li>
            </ul>
        </div>
    </form>

And also you have an error in the function name, it's getClientOriginalExtension() not getClientOriginalExtensions().