Skip to content
Advertisement

Laravel 7 tag is not displayed

I do a udemy course

I have one problem

When I start to create a post I have to insert a photo

When I add a photo, the photo is not displayed

While the “View Source Page” clearly shows that the photo is inserted and should be displayed but not displayed

I wrote the code li

<img src="{{ $post->image }}" alt="">

I tried to add this extension as well but it didn’t work again

<img src="{{ asset($post->image) }}" alt="">

View Source Page

<img src="posts/2F2K2uHwvdL11pB8FSmecTeKCCNe1qIaLjCTcOPU.jpeg" alt="">

PostController

    public function create()
    {
        return view('posts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(CreatePostsRequest $request)
    {
        $image = $request->image->store('posts');

        Post::create([
            'title'       => $request->title,
            'description' => $request->description,
            'content'     => $request->content,
            'image'       => $image
        ]);

        session()->flash('success','Post is successfully created');

        return redirect(route('posts.index'));

    }

CreatePostsRequest

class CreatePostsRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title'       => 'required|unique:posts',
            'description' => 'required',
            'image'       => 'required|image',
            'content'     => 'required'
        ];
    }
}

Post.php

class Post extends Model
{
    protected $fillable = [
        'title','description','content','image','published_at'
    ];
}

If anyone can help me for a good part of the night I fight this but I fail

Thank you in advance

Advertisement

Answer

You’re not utilising the full storage path name, as seen in your View Page Source.

First you need to make sure your storage and public folders are linked by using the artisan link command:

php artisan storage:link

Next you can specifiy the public directory with your store method:

$image = $request->image->store('public/posts');

Now you can access your image with:

<img src="{{asset('storage/posts/' . $post->image)}}" alt="">
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement