phayes0289's avatar

How to Display a short List of Posts on a "Home Dashboard Page"

This may be Laravel 101, but I am a total newbie and have not seen an example of this yet. I have a full blog post view working with pagination that displays all posts. This is NOT the home page of the project. On the home page of the project, I want to display the latest 20 posts with their titles, featured image and excerpt. In addition, I want there to be many other multiple data objects used on the same page... as in a Dashboard. In terms of best practices, what is the correct way to do this in Laravel?

I totally get the the idea of having a view, controller, model, factory etc dedicated to posts. But how do you mix things up on a page that includes smaller bit size data objects from other parts of the project, as in a dashboard?

0 likes
5 replies
Anthonysb's avatar

with just Laravel you would do it by doing the same eloquent query on your dashboard index controller as on your post controller, just make sure you add use App\Model\Posts; so the model is available to the controller

$posts = posts::orderBy('created_at', 'desc')->get();

$dbposts = $posts->limit(20)->get();

then pass it to your view via compact, then you can run your for each loop for the posts, if you are trying to make it available on every page that is using a dashboard component then you could use a view composer and pass it that way as well

I think this is what your asking but if not sorry

1 like
Talinon's avatar
Talinon
Best Answer
Level 51

@phayes0289 You just need to pass all that additional information to the view.

public function index() 
{
	return view('dashboard')->with([
		'posts' => Post::latest()->limit(20)->get(),
		'someOtherData' => Model::filters()->get(),
		'foo' => 'bar',
		...
		...
	]);
}

Above, I showed how you can retrieve the most recent 20 Posts.

In the view, you can access the data via the variable names, $posts, $someOtherData, $foo, etc.

2 likes
phayes0289's avatar

Thank you both for your response. I can see how both responses are similar. Talinon's detailed example code just made it a little easier on me being a newbie.

aiMeta's avatar

Hi all, I am able to get posts (list) into the preview blade (home dash) yet I cant find some knowledge base to help me reduce the initial word count displayed, its all overlapping each other .. your direction would be greatly appreciated. Here is my code:

<x-public-layout>
    <x-slot name="title">
        @if (isset($pageTitle))
            {{ $pageTitle }}
        @else
            {{ $site_name }}
        @endif
    </x-slot>
    <div class="flex flex-col space-y-20 px-4 lg:px-40 py-4">
        @foreach ($articles as $article)
            <div class="grid grid-cols-1 lg:grid-cols-5 gap-8 h-56">
                <div
                    @class([
                        'lg:col-span-2',
                        'lg:order-last' => $loop->odd,
                    ])
                >
                    @if ($article->getMedia()->count())
                        <a href="{{ $article->path() }}">
                            <img src="{{ $article->getFirstMediaUrl() }}" class="rounded-md shadow-md filter drop-shadow-md w-full object-cover mt-0">
                        </a>
                    @endif
                </div>
                <div
                    @class([
                        'lg:col-span-3' => $article->getMedia()->count(),
                        'lg:col-span-5' => ! $article->getMedia()->count(),
                    ])
                >
                    <div class="flex justify-between text-xs">
                        <a class="text-red-700 font-bold hover:text-black" href="{{ route('categories.public', $article->category) }}">
                            {{ $article->category->name }}
                        </a>
                        <span>
                            {{ $article->published_at->format('dS M Y') }}
                        </span>
                    </div>
                    <a href="{{ $article->path() }}" class="hover:text-red-800">
                        <h2 class="font-bold text-2xl mt-2 mb-4">
                            {{ ucwords($article->title) }}
                        </h2>
                    </a>
                    <div class="prose prose-red mb-2">
                        {!! $article->first_paragraph !!}
                    </div>
                    <small>
                        @foreach ($article->tags as $tag)
                            <a class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800 hover:text-black" href="{{ route('tags.public', $tag) }}">
                                {{ $tag->name }}
                            </a>
                        @endforeach
                    </small>
                </div>
            </div>
        @endforeach
    </div>
    {{ $articles->links() }}
</x-public-layout>

Please or to participate in this conversation.