You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel Nova资源创建页面显示自定义消息?

Hey there! I've got a few solid ways to add custom messages to your Laravel Nova resource's create page—let's break them down based on your needs:

1. Quick & Simple: Use Field Help Text

If your message ties to a specific field, or you just need a quick way to add a note near form elements, Nova's built-in help() method is perfect. You can even use HTML to format the text for emphasis:

// In your Nova resource file (e.g., app/Nova/Post.php)
public function fields(Request $request)
{
    return [
        Text::make('Title')
            ->help('<strong>Heads up!</strong> Keep titles under 100 characters to avoid truncation.'),
        
        // Want a standalone message? Use a read-only text field hidden from other views
        Text::make('Important Reminder')
            ->readonly()
            ->help('Double-check all required fields before submitting—category can\'t be changed after saving!')
            ->hideFromIndex()
            ->hideFromDetail(),
    ];
}
2. Standout Message: Custom Nova Card

For a more prominent, standalone message (like a top/bottom banner on the create page), a custom Nova Card is the way to go. It's flexible and keeps your logic separate from field definitions.

Step 1: Generate the card

php artisan nova:card CustomCreateMessageCard

Step 2: Customize the card's Vue component

Edit nova-components/CustomCreateMessageCard/resources/js/components/Card.vue:

<template>
    <div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
        <h3 class="text-lg font-medium text-blue-800">Create Resource Guidelines</h3>
        <p class="mt-2 text-sm text-blue-700">
            All entries must include a valid URL and description. Unapproved content will be removed within 24 hours.
        </p>
    </div>
</template>

<script>
export default {
    mounted() {
        // Only show this card on the create page
        if (this.$route.name !== 'create') {
            this.$el.style.display = 'none';
        }
    }
}
</script>

Step 3: Register the card in your resource

// In your Nova resource file
public function cards(Request $request)
{
    // Conditionally show the card only on the create page
    if ($request->is('nova/resources/*/create')) {
        return [
            new \App\Nova\Cards\CustomCreateMessageCard(),
        ];
    }

    return [];
}
3. Sidebar Message: Custom Nova Tool

If you prefer to place your message in the sidebar, a custom Tool works great. Here's how to set it up:

Step 1: Generate the tool

php artisan nova:tool CreatePageReminderTool

Step 2: Restrict visibility to create pages

Edit the tool's ToolServiceProvider.php (in app/Nova/Tools/CreatePageReminderTool/):

public function renderNavigation()
{
    // Only show the sidebar tool on create pages
    if (request()->is('nova/resources/*/create')) {
        return view('create-page-reminder-tool::navigation');
    }
    return '';
}

Step 3: Add your message to the tool's view

Edit the navigation view file (in resources/views/vendor/create-page-reminder-tool/navigation.blade.php) to include your custom text.

If you need complete control over the page layout, you can publish Nova's views and edit the create page template directly. Note: This will prevent automatic view updates when you upgrade Nova, so use this only if other methods don't work.

Step 1: Publish Nova views

php artisan vendor:publish --tag=nova-views

Step 2: Edit the create page template

Open resources/views/vendor/nova/resources/create.blade.php and add your message wherever you want (e.g., right after the page header):

<div class="mb-6 bg-yellow-50 border border-yellow-200 rounded-lg p-4">
    <p class="text-yellow-700">⚠️ This is a site-wide reminder for all new resource submissions.</p>
</div>

内容的提问来源于stack exchange,提问作者Jigar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:35:24