Transform your app with proper styling using Blade components. Create reusable UI components and format timestamps to build a polished social media feed.
Alright! Our application is starting to take shape, but let's be honest—it looks pretty basic right now. This is just the start of having our full Chirper application, but let's give it some TLC. We're going to add user avatars, format some timestamps, and use DaisyUI's components to make this look just a little bit nicer.
Let's transform this into something that actually looks like a social media feed!
Step 1: Create a Chirp Component
The first thing I want to tackle is this home.blade.php file. It's great, but it'd be nice if we could get every chirp as its own component. That way we only have one place where we're modifying how chirps are displayed.
Blade components aren't just for layouts—they're perfect for reusable UI pieces like this! Let's create a component for displaying individual chirps:
Create a new file at resources/views/components/chirp.blade.php:
Let me walk you through what we changed here. This is pretty close to what we had previously, but now we're just making it a little bit cleaner.
Notice a few key things:
Avatar magic: If there is a chirp user, we're using avatars.laravel.cloud, which is a free service that generates these neat little gradients for you whenever you need avatars for your application. We're just passing in the user's email to generate them specifically for that user.
Anonymous fallback: We have a default avatar for anonymous users too.
Props power: Here's the neat thing about Blade components—we can pass in a prop (in this case, a chirp model instance). This prop then allows us to access $chirp->user, $chirp->message, $chirp->created_at, and so on when we use this component in our views.
Note: We're handling both authenticated and anonymous chirps. In lesson 11, we'll add authentication so every chirp has a real user!
Step 2: Update the Feed View
Now that we have our chirp component, let's update home.blade.php to use it. We're still looping through all the chirps like we did before—we're just doing it with our new component instead.
Notice that we're passing in the :chirp prop to our <x-chirp> component—that colon indicates it's a prop we can pass into the component. Pretty neat, right?
Step 3: Add Sample Data
Now that we're cruising along, why don't we get some more sample data without having to open php artisan tinker every time we want to create chirps? Laravel gives us an easy way to do this with something called seeders.
Let's create a seeder to generate some sample chirps. Run:
1php artisanmake:seederChirpSeeder
php artisan make:seeder ChirpSeeder
Now, let's edit database/seeders/ChirpSeeder.php. Seeders are a great way to make sample data so we can make sure our application is looking and feeling and working the way we expect.
We won't get into factories in this particular course, but factories are a great way to make random data. Seeders, on the other hand, are going to be a little bit more specific—they're basically just telling our database to run particular commands. You can think of seeders as an automated way to run php artisan tinker commands, so you don't have to type it all out.
1<?php
2
3namespace Database\Seeders;
4
5use App\Models\User;
6use App\Models\Chirp;
7use Illuminate\Database\Seeder;
8
9classChirpSeederextendsSeeder
10{
11publicfunctionrun():void
12 {
13// Create a few sample users if they don't exist
36'Just discovered Laravel - where has this been all my life? 🚀',
37'Building something cool with Chirper today!',
38'Laravel\'s Eloquent ORM is pure magic ✨',
39'Deployed my first app with Laravel Cloud. So smooth!',
40'Who else is loving Blade components?',
41'Friday deploys with Laravel? No problem! 😎',
42 ];
43
44// Create chirps for random users
45foreach ($chirpsas$message) {
46$users->random()->chirps()->create([
47'message'=>$message,
48'created_at'=>now()->subMinutes(rand(5,1440)),
49 ]);
50 }
51 }
52}
<?php
namespace Database\Seeders;
use App\Models\User;
use App\Models\Chirp;
use Illuminate\Database\Seeder;
class ChirpSeeder extends Seeder
{
public function run(): void
{
// Create a few sample users if they don't exist
$users = User::count() < 3
? collect([
User::create([
'name' => 'Alice Developer',
'email' => '[email protected]',
'password' => bcrypt('password'),
]),
User::create([
'name' => 'Bob Builder',
'email' => '[email protected]',
'password' => bcrypt('password'),
]),
User::create([
'name' => 'Charlie Coder',
'email' => '[email protected]',
'password' => bcrypt('password'),
]),
])
: User::take(3)->get();
// Sample chirps
$chirps = [
'Just discovered Laravel - where has this been all my life? 🚀',
'Building something cool with Chirper today!',
'Laravel\'s Eloquent ORM is pure magic ✨',
'Deployed my first app with Laravel Cloud. So smooth!',
'Who else is loving Blade components?',
'Friday deploys with Laravel? No problem! 😎',
];
// Create chirps for random users
foreach ($chirps as $message) {
$users->random()->chirps()->create([
'message' => $message,
'created_at' => now()->subMinutes(rand(5, 1440)),
]);
}
}
}
These are just some sample chirps in an array, and we're saying if there are fewer than three users (which there currently are in the database), let's go ahead and create some new users with this information. Again, this is exactly what we would do within tinker—this is just making it a little bit easier for us.
For each chirp message, every user is going to get a random chirp based off of this chirps array. Pretty cool, right?
Now let's run the seeder:
1php artisandb:seed--class=ChirpSeeder
php artisan db:seed --class=ChirpSeeder
Running the seeder command is as simple as that! A database was successfully seeded.
Again, a seeder is just an easier way to have all those tinker commands run for us, but within a way that Laravel expects. In this case, we're just running all that PHP code, but seeding the database with it.
Step 4: Real-time Updates (Preview)
Your feed looks great, but I want to make you aware of something we're not going to touch in this course, but something you might want to look into in the future: keeping this form up to date in real-time.
Right now, you can see that when we post a chirp, it says "0 seconds ago." But if anyone else accesses this page, they won't see new chirps until they actually refresh the page. Laravel has some great options for this, such as WebSockets with something like Laravel Reverb, which can notify all users when a new chirp comes in.
Here's what Laravel offers for real-time features:
Laravel Broadcasting - Push updates to browsers in real-time
Laravel Echo - JavaScript library for receiving broadcasts
Pusher or Laravel Reverb - WebSocket servers for real-time communication
Again, not something we're going to be touching on in this course, but it's helpful to know that those options are out there and might be a great way to build on top of what we're building here with Chirper!
What We've Built
Look at that! Your feed now has:
A reusable Blade component for chirps
Beautiful user avatars generated specifically for each user based on their email
User information and formatted timestamps
A polished, professional design with DaisyUI
Handles empty states gracefully
A clean way of generating sample data with seeders
So we're really moving right along! We now have a reusable Blade component for chirps, a way to generate chirps if we ever need to reset the database and start it up again, and a clean way of showing user information.
But it's still read-only. Next up: since it's still read-only, we're going to let users create their own chirps. Time to make this social network actually social!