zernzern1993
zernzern1993

Reputation: 265

Trying to display image using (img src) tag, the path of the image is saved in my database

I have successfully saved the image that is upload from the user, into a field called 'image' in my 'advertisement' table. But I am having a problem displaying the image on my view (blade.php) file.

I tried searching for solutions online but none of which worked for mine...

Below is the store method for my AdvertisementsController:-

public function store(Request $request)
{
    // $media = Media::all();

    request()->validate([
        'image' => 'required',
        'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    //find current admin's id
    $admin_user = auth()->id();

    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $image_name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
        $folderPath = public_path('/uploads/images');
        $imagePath = $folderPath. "/".  $image_name;
        $image->move($folderPath, $image_name);
    }

    $title = request('title');
    $description = request('description');
    $sort_num = request('sort_num');

    $media = Media::create([
        'title' => $title,
        'description' => $description,
        'sort' => $sort_num,
        'created_by' => $admin_user,
    ]);

    Advertisement::create([
        'image' => $imagePath,
        'expired_at' => Carbon::now()->format('Y-m-d H:i:s'),
        'media_id' => $media->id,
    ]);

    // return back()->with('success', 'Your advertisement has been successfully created!');
    return redirect('/advertisements');

}

I can save the path successfully but the path that is saved is quite long, e.g. : "/Users/hannzern/Desktop/numericledger-backend/public/uploads/images/klang-river.jpeg" and thought the path would be saved as "/public/uploads/images/klang-river.jpeg". Could this be the problem?

Below is the code for my view (blade.php):-

<h1>Advertisements</h1>

<ul>
    @foreach ($advertisements as $advertisement)
        <a href="/advertisements/{{$advertisement->id}}">
            {{ $advertisement->media->title }}   
        </a>
        <br>

        //Three methods of retrieving the image from the path but all three not working
        <img src="{{ $advertisement->image }}" height="300" width="200">

        <img src="/Users/hannzern/Desktop/numericledger-backend/public/uploads/images/klang-river.jpeg" height="300" width="200">

        <img src="/public/uploads/images/klang-river.jpeg" height="300" width="200">

        <br>
        <br>
        <br>
        <br>
    @endforeach

</ul>

<a href="/advertisements/create">Add new advertisement</a>

Also, from the file directories, I can see the "klang-river.jpeg" image in my /public/uploads/images folder... Anyone have any idea why? :(

Upvotes: 0

Views: 38

Answers (1)

thisiskelvin
thisiskelvin

Reputation: 4202

/public will be resolved by using / within links in the browser, so your link should be saved into the database as:

/Users/hannzern/Desktop/numericledger-backend/uploads/images/klang-river.jpeg

However, to make sure that this works whereever you have host your application, you should not save the absolute path. Only save the following into the database:

klang-river.jpeg

Then, using the asset() helper, you can call the correct url in your template:

<img src="{{ asset('uploads/images/' . $advertisement->image) }}" height="300" width="200">

This will resolve to the expected url. I hope this helps.

Upvotes: 2

Related Questions