lukbanbernard
2 months ago
320
5
Laravel

How do I put a default image if user did not provide an image

Posted 2 months ago by lukbanbernard

This is my function

public function store(Request $request)
    {
        $post = new Post;

        if ($request->hasFile('images')) {
            $path = Storage::putFile('public', $request->file('images'));
            $post->image = Storage::url($path);
        }

        $post->title = $request->title;
        $post->content = $request->content;

        auth()->user()->posts()->save($post);
        
        return redirect()->route('feedbackpage');
    }

This is my HTML

<a href="{{route('post_path', [$post->id])}}"><img src="{{asset($post->image)}}" class="card-img-top"></a>

Please sign in or create an account to participate in this conversation.