Home > Software engineering >  How can I display Laravel images on a live server?
How can I display Laravel images on a live server?

Time:11-16

I developed a laravel app which is working perfectly on my local machine together with the images. On deployment to live server, the images are no longer visible. I am using shared hosting and my folder structure is like this: public_html containing the public files like upload and main containing the remaining files which should not be visible to the user. Here is my controller `public function

PortfolioAdd(){
        return view('frontendbackend.portfoliosection.addportfolio');
    }

    public function PortfolioStore(Request $request){

            $validatedData = $request->validate([
                'title' => 'required:portfolio_sections,title',
                'description' => 'required:portfolio_sections,description',
            
            
        ]);

        $data = new PorfolioSection();
        $data->title = $request->title;
        $data->description = $request->description;

        if ($request->file('image')) {
            $file = $request->file('image');
            $filename = date('YmdHi').$file->getClientOriginalName();
            $file->move(public_path('upload/portfolio_images'),$filename);
            $data['image'] = $filename;
        }
     
        $data->save();

`and here is my view

                  <table id="example1" class="table table-bordered table-striped" style = "color:white">
                    <thead>
                    <tr>
            <th width="5%" style = "color:white">SL</th>
            <th style = "color:white">Title</th>
            <th style = "color:white">Description</th>
            <th style = "color:white">Image</th>
            <th style = "color:white">Action</th>    
        </tr>
    </thead>
    <tbody> 
    @foreach($allData as $key => $portfolio )
        <tr>
            <td style = "color:white"> {{ $key 1 }} </td>
            <td> {{ $portfolio->title }} </td>
            <td> {{ $portfolio->description }} </td>
            <td> 
                
                <img src="{{ (!empty($portfolio->image))? url('upload/portfolio_images/'.$portfolio->image):url('upload/no_image.jpg') }}" style="width: 60px; width: 60px;"> 
                           </td>    

            
            <td>
            <a href="{{route('view.portfolio.edit', $portfolio->id)}}" class="btn btn-info">Edit</a>
            <a href="{{route('view.portfolio.delete', $portfolio->id)}}" class="btn btn-danger" id="delete">Delete</a>

            </td>
        </tr>
        @endforeach
                    </tbody>
                    <tfoot>
                         
                    </tfoot>
                  </table>

CodePudding user response:

First check whether ($portfolio->image) is showing any value by passing it directly in the tag i.e

{{ $portfolio->image}}

if it returns values, check whether the value exists in your folder. If it exists ch then check your image src.

CodePudding user response:

I would try the following(have not tested)

change this: $file->move(public_path('upload/portfolio_images'),$filename);

to: $file->move(public_path() . 'upload/portfolio_images',$filename);

CodePudding user response:

You must create a symbolink link for that. The default folder for uploading files is the storage folder. By running php artisan storage:link, the application will generate a symbolic link that files will be accebile from the public area. Google symbolic link. You will find the answer.

  • Related