aondoe's avatar

Files don't upload to the

Hello. I'm trying to upload files into the public folder. I've already created a link to the storage folder using artisan. But for some reason, the file title uploads to the MySQL field. Has anyone else experienced this?

The main code is in the if-statement near the bottom of the code.

Here is my Controller

    public function store(Request $request){
                request()->validate([
                    'title' => 'required|min:2',
                    'subject' => 'required|min:4',
                    'content' => 'required|min:50',
                    'featured_img' => 'sometimes|image'
                ]);

                $this->validate($request, [
                    'title'=>'required|min:3',
                    'content'=>'required|min:10',
                    'featured_image' => 'sometimes|image'
                ]);
                
                PostIt::create(['title'=>$request->title,
                                'content'=>$request->content,
                                'featured_img' => $request->featured_img
                ]);

                if ($request->hasFile('featured_img')){
                    

                    $path = $request->file('featured_img');
                    // $fileName=$featured_img->getClientOriginalExtension();
                    $image->move(base_path('/images'), $fileName);
                    $post->update(array_merge($request->all(), ['featured_img' => $path]));
                    
                    return "Here is the path: ".$path;
                }
                return redirect()->route('posts.index');
}

Here is my form which contains the upload field

<form onsubmit="return confirm('Are you sure you want to edit the post {{$post->title}}?')" action="{{route('posts.update', $post->id)}}" method="POST" type="multipart/form-data">
                    @method('put')
                    @csrf
            <div class="form-group">
                        <label for="title" >Title</label>
                        <input type="text" name="title" id="title" class="form-control" value="{{$post->title}}">
            </div>
        <div class="form-group">
                    <h2>Your current subject is {{$post->subject}}</h2>
            </div>
            
        <div class="form-group">
                <input type="file" name="featured_img">{{$post->featured_img}}
        </div>

Any help would be great. Thanks

0 likes
2 replies
Cronix's avatar

https://laravel.com/docs/5.7/requests#storing-uploaded-files

$path = $request->file('featured_img');

That's the actual file, not the path to the saved file (you didn't save it - see the docs)

Edit: same thing here

PostIt::create(['title'=>$request->title,
    'content'=>$request->content,
    'featured_img' => $request->featured_img
]);

You're just dumping the actual file into the db, not the path of the saved file (you didn't save the file as a file on the filesystem there, either).

Why do you have 2 sets of validation rules running?

aondoe's avatar

Thanks for your tips. I forgot to remove one of the validation rules. I will read the link you posted thanks.

Please or to participate in this conversation.