oggiesutrisna
oggiesutrisna

Reputation: 19

No Image showing in a home Laravel

in laravel, i have to link the storage using php artisan storage:link so i can show the image that when user to post a article with a image, the image will be shown. but for now, the image didn't show up. How do i fix it.

here's the code from views/post/index.blade.php

<tbody>
            @foreach($posts as $post)
            <tr>
                <td>
                    <img src="{{ asset($post->image) }}" alt="">
                </td>
                <td>
                    {{ $post->title }}
                </td>
                <td>
                    <a href=" {{ route('categories.edit', $post->category->id) }}">
                        {{ $post->category->name }}
                    </a>
                </td>

when the user post a image, the image will be stored in the linked storage that i've doing before. so here's the code from postscontroller.php if i change the 'posts' to 'post' it says error. because i didn't suitable for the function 'posts' that i write before.

public function store(CreatePostRequest $request)
{
    // upload image omegalul
    $image = $request->image->store('posts');
    // create the post
    $post = Post::create([
        'title' => $request->title,
        'description' => $request->description,
        'content' => $request->content,
        'image' => $image,
        'published_at' => $request->published_at,
        'category_id' => $request->category,
        'user_id' => auth()->user()->id,
    ]);

    if ($request->tags) {
        $post->tags()->attach($request->tags);
    }
    // flash a message
    session()->flash('success', 'Post created successfully');
    // redirect the user
    return redirect(route('posts.index'));
}

if you guys see any problems that i didn't appeared here, just ask me. Thank you guys so much

Upvotes: 0

Views: 61

Answers (1)

Tarsis Cruz
Tarsis Cruz

Reputation: 133

If you are working with Laravel Storage it's recommended to use the storage functions to store, get or delete media files.

Use theses commands to do it:

To store:

Storage::disk(config('filesystems.default'))->put($path, $file);

To get:

Storage::disk(config('filesystems.default'))->url($file);

To remove:

Storage::disk('local')->delete($path);

Your code will be like this:

<tbody>
        @foreach($posts as $post)
        <tr>
            <td>
                <img src="{{ Storage::disk(config('filesystems.default'))->url($post->image) }}" alt="">
            </td>
            <td>
                {{ $post->title }}
            </td>
            <td>
                <a href=" {{ route('categories.edit', $post->category->id) }}">
                    {{ $post->category->name }}
                </a>
            </td>

And:

public function store(CreatePostRequest $request)
{
    // upload image omegalul
    $image = Storage::disk(config('filesystems.default'))->put('posts', $request->image);

Note: Don't forget to check if your filesystem is setted like 'local' - if you are using local storage.

Upvotes: 1

Related Questions