ralphdns's avatar

my update method cannot display updated images to the index view

see my PostsController@update method

    {
        
        //1. saving everytin frm the $request obj frm the form
        $input = $request->all();

        //2. storing the filename in the object, into $file variable, that is if the user actually choose a file
        if($file=$request->file('cover_image')){

            //3. storing the name of the file
            $filenameWithExt = $file->getClientOriginalName();

            //get just filename using php
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);

             //get just extension
            $extension = $request->file('cover_image')->getClientOriginalExtension();

              //filename to store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;

            //upload the image
            $path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
            

            //5. equal the path column in db, to equal the filename
           /*  $input['cover_image'] = $fileNameToStore; */

            
        } /* else {
            $fileNameToStore = 'noimage.jpg';//if no image is deliberately selectd, use this deafault image
        } */

        //find the post->id sent frm the edit form
        $post = Post::findOrFail($id);

        if($request->hasFile('cover_image')){
            $input['cover_image'] = $fileNameToStore;
        }

        //update the object, by passing all values into it
        $post->update($input);
    
        return redirect('/posts');

        
    } ```
 
see my index.view
```@extends('layouts.app')

@section('content')
    <h1>All Posts Page</h1>
    <ul>
        @foreach($posts as $post)
            <li class="list-group-item">
                <div class="img-style">
                    <img class="img-responsive" width="30%" src="/storage/cover_images/{{$post->cover_image}}" alt="">
                </div>
                <a href="{{route('posts.show', $post->id)}}">{{$post->title}}</a> <br>
                    {{$post->content}}
            </li><br><br>
        @endforeach
    </ul>
@endsection ```
0 likes
0 replies

Please or to participate in this conversation.