1

In my ImageController, I've moved an image to a directory and saved the image path in a table in database as,

public function store(Request $request)
{
    $new_country = new SelectCountry();
    $message = [
        'required' => "This field can not be empty",
    ];

    $this->validate($request, [
        'country_name' => 'required',
        'alternate_title' => 'required',
        'country_flag' => 'required',
    ], $message);


    $new_country->country_name = $request->country_name;
    $new_country->alternate_title = $request->alternate_title;
    if($request->hasfile('country_flag'))
    {
        $country_flag_image = $request->file('country_flag');

        $country_flag_name = $country_flag_image->getClientOriginalName();

        $extention = $country_flag_image->getClientOriginalExtension();

        $dirpath = public_path('assets/images/country/');
        $country_flag_image->move($dirpath,$country_flag_name);

        $new_country->country_flag_path = $dirpath.$country_flag_name;
    }
    $new_country->save();

    return redirect()->route('admin.country');
}

Now I want to use the path and display the image along other informations. I've used the following code to return view,

public function select_country() 
{
    $countries = SelectCountry::all();
    return view('auth.select_country')->with('countries',$countries);
}

And in the view, I've used following code to display datas stored in the database,

<table class="table-hover table-responsive container-fluid col-xs-12 col-sm-12">        
    <thead>
        <tr>
            <th>Country Name</th>
            <th>Alternate Title</th>
            <th>Country Flag</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
            @foreach($countries as $country)
          <td>{{ $country->country_name }}</td>
          <td>{{ $country->alternate_title }}</td>
          <td><img src="{{ $country->country_flag_path }}"></td>
          <td>
            <a type="button" href="" class="btn btn-warning btn-xs">Update</a>
            <a type="button" href="" class="btn btn-danger btn-xs">Delete</a>
          </td>
        @endforeach
    </tbody>        

The problem is the image is not displayed while other informations are displayed. The src attribute in img tag is returning path but not directory.

If there is any solution, please tell me. Thanks.

6
  • Have you tried using the dd($country); function to see what information is being passed to the view? Commented May 8, 2016 at 12:25
  • Yes I did. It returns country_name and alternate_title but not the image. Instead of image, it returns whole directory path as: "country_flag_path" => "C:\Users\Ashok\Desktop\Laravel\LaravelConsultancy\public/assets/images/country/canada.png" Commented May 8, 2016 at 12:33
  • I think public_path('assets/images/country/'); is your issue. Try setting it to just $dirpath = "images/country" as Laravel looks inside public by default. You can then reference it by doing src="{{ asset($country->country_flag_path) }} Commented May 8, 2016 at 12:36
  • It worked. Thanks a lot. Commented May 8, 2016 at 12:42
  • Hi there, as the solution has worked for you please can you click the tick next to my answer to mark it as accepted? Thank you :) Commented May 8, 2016 at 19:48

1 Answer 1

1

I think your issue lies with where you are setting the storage folder. Laravel with automatically reference the public folder for storage of images etc so there's no need for the public_path function.

Try setting your $dirpath to just $dirpath = "/asset/images/country" as Laravel looks inside public by default. You can then reference it by doing <img src="{{ $country->country_flag_path }}">

Then upload a new image and see what the database has the path set to, hopefully it should be a bit more normal :)

Please note that my original answer of:

Try setting your $dirpath to just $dirpath = "images/country" as Laravel looks inside public by default. You can then reference it by doing <img src="{{ asset($country->country_flag_path) }}">

Should still work as it's how I've always done it :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.