0

I am trying to understand how to retrieve a record from the database which was submitted by a specific user and display that one single record. Currently, I am retrieving all submitted posts with a simple forelse loop. The idea is to display this information in Bootstraps card title view (photo below). Ultimately, as the user posts more content the view layout would look something like this https://getbootstrap.com/docs/4.0/examples/album/

As the user adds more posts I would like to retrieve them all but display each post in a single Card title as below. Can anyone help me understand this process to get this ultimately working?

enter image description here

@foresle loop

@forelse($posts as $post)
    <p><strong>
            <a href="/posts/{{ $post->id }}">{{ $post->title }}</a>
            </strong></p>
@empty
    <p>No Posts Currently</p>
@endforelse 
2
  • 1
    You can copy the html from the bootstrap 4 docs inside your forelse, and replace the content of card-title by {{ $post->title }} and the same for the <img> src, <a> href and so... Commented Apr 24, 2020 at 17:14
  • what's your opinion about using foreach() instead? Commented Apr 24, 2020 at 17:15

1 Answer 1

1

You can achieve this type of view using the following code:

<div class="row">
    @forelse($posts as $post)
    <div class="col-md-4">
        <div class="card mb-4 box-shadow">
            <img class="card-img-top"
                 src="{{ $post->image }}"
                 data-holder-rendered="true">
            <div class="card-body">
                <p class="card-text">{{ $post->title }}</p>
                <div class="d-flex justify-content-between align-items-center">
                    <div class="btn-group">
                        <a href="/posts/{{ $post->id }}" class="btn btn-sm btn-outline-secondary">View</a>
                    </div>
                    <small class="text-muted">9 mins</small>
                </div>
            </div>
        </div>
    </div>
    @empty
        <p>No Posts Currently</p>
    @endforelse
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

You're very much welcome - there are many more fun things to do with Laravel. Have fun and keep coding 💪
Hi Chris, quick question, for ``` <small class="text-muted">9 mins</small>``` whats the best way to show this for actual time of submission? any suggestions?

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.