Home > Software engineering >  Laravel image not displaying when using form
Laravel image not displaying when using form

Time:10-25

Im trying to get image input from use using a form. I followed some youtube tutorial and follow the method to store the image path into my storage/app/images directory. However, when i try to display the images in my views it is not showing. Can anyone point me in the right direction? thankyou in advance

My form

 <form action="{{ url('publish-news') }}" method="post" enctype="multipart/form-data">
            @csrf

            <div >
                <input type="text" name="title"  id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter title" >
            </div>


            <textarea id="content" name="content"> </textarea>
            <div >
                <input name="image"  type="file" id="formFile">
            </div>

            <button type="submit" >Publish</button>

            <script>
                var desc = CKEDITOR.instances.DSC.getData();
            </script>

        </form>

My controller

 public function addnews(Request $request)
    {
        $request -> validate([
            'title' => 'required',
            'content' => 'required',
            'image' => 'required'

        ]);

        $content = $request->input('content');
        $title = $request ->input('title');
        $image = $request -> file('image');


            if ($image)
            {
                $newfile = $request->file('image');
                $file_path = $newfile->store('images');
                $timestamp = \Carbon\Carbon::now()->toDateTimeString();
                $data=array('title'=>$title,"content"=>$content,"image"=>$file_path,"timestamp"=>$timestamp);
                $query = DB::table('announcement')->insert($data);

                return view('admin.admin-announcement');


            }

    }

My view (i want to display the images here)

 <div >
            @foreach($announcements as $announcement)
                <div  style="width: 18rem;">
                    <img  src="{{asset('storage/'.$announcement->image)}}" alt="Card image cap">
                    <div >
                        <h5 > {{$announcement->title}}</h5>
                        <a href="#" >Go somewhere</a>
                    </div>
                </div>
            @endforeach
        </div>

My directory [![https://i.stack.imgur.com/NznNp.png][1]][1]

CodePudding user response:

You can save your files wherever you want within your Laravel application. Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks".

Files are saved under the folder path named storage by default. You should create a symlink to your storage folder so that you can access it easier. The symlink makes files stored in the storage directory accessible from the web.

Execute this command:

php artisan storage:link

see also: https://laravel.com/docs/9.x/filesystem#the-public-disk

  • Related