Step 1: Install Laravel Nova Swatches
Firstly you will have to install nova-swatches using the composer package manager and publish the configuration file.
composer require ynacorp/nova-swatches
php artisan vendor:publish --tag=config --provider=Yna\\NovaSwatches\\FieldServiceProvider
Step 2: Call the Field in Nova Resource
For this example imagine you have a Nova resource called "Post" and for each of the posts you have a unique column called "color" that will store the HEX color code. Below is the database column definition.
- id
- title
- body
- color
- created_at
- updated_at
<?php // app/Nova/Post.php use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use Laravel\Nova\Fields\Trix; use Yna\NovaSwatches\Swatches; public function fields(Request $request) { return [ ID::make(__('ID'), 'id')->sortable(), Text::make('Title')->rules('required'), Trix::make('Body'), Swatches::make('Color', 'color'), ]; }
php artisan nova:resource Post
Step 3: Nova Swatch Configuration
By default the swatch field has a basic configuration which only shows the color swatch, this can be updated to add other options from the config file located in "config/nova/swatches.php".
<?php // config/nova/swatches.php return [ /** * Props to pass to the vue-swatches component. * * See https://saintplay.github.io/vue-swatches/#sub-props */ 'props' => [ 'colors' => 'material-basic', 'show-fallback' => true, 'fallback-type' => 'input', ] ];
Step 4: Create Multiple Post With Different Color Accent and Render to Blade
Once you have created multiple posts with different color accents, now you can render the post from the Laravel blade and customize the post styling. For the code below, we'll be using PostSrc Basic Card Component.
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 w-full"> <div class="relative p-4 w-full bg-white rounded-lg overflow-hidden hover:shadow" style="min-height: 160px;"> <div> <div class="relative block h-full"> <div class="h-32 bg-gray-100 rounded-lg"></div> </div> </div> <h2 class="mt-4 text-gray-800 text-sm font-semibold line-clamp-1"> Card Title </h2> </div> <div class="relative p-4 w-full bg-white rounded-lg overflow-hidden hover:shadow" style="min-height: 160px;"> <div> <div class="relative block h-full"> <div class="h-32 bg-gray-100 rounded-lg"></div> </div> </div> <h2 class="mt-4 text-gray-800 text-sm font-semibold line-clamp-1"> Card Title </h2> </div> <div class="relative p-4 w-full bg-white rounded-lg overflow-hidden hover:shadow" style="min-height: 160px;"> <div> <div class="relative block h-full"> <div class="h-32 bg-gray-100 rounded-lg"></div> </div> </div> <h2 class="mt-4 text-gray-800 text-sm font-semibold line-clamp-1"> Card Title </h2> </div> </div>
Now that you have copy the code you can create a new blade partially or just loop your $posts and apply the "color" swatch to make each post unique. For this example let's make each post have a different border color.
border: 2px solid {{ $post->color }}
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 w-full"> @foreach($posts as $post) <div class="relative p-4 w-full bg-white rounded-lg overflow-hidden hover:shadow" style="min-height: 160px; border: 2px solid {{ $post->color }}"> <div> <div class="relative block h-full"> <div class="h-32 bg-gray-100 rounded-lg"></div> </div> </div> <h2 class="mt-4 text-gray-800 text-sm font-semibold line-clamp-1"> Card Title </h2> </div> @endforeach </div>
Leave a reply