Advanced

MoonShineJsonResponse

Basics

MoonShineJsonResponse extends Illuminate\Http\JsonResponse and is supplemented with helper methods for interacting with the frontend part of the admin panel interface after processing a request.

Methods

Toast

The toast() method triggers a standard toast notification of the admin panel.

toast(string $value, ToastType $type = ToastType::DEFAULT, null|int|false $duration = null)

Example:

MoonShineJsonResponse::make()->toast('My message', ToastType::SUCCESS, duration: 3000);

Redirect

The redirect() method will redirect to the specified URL.

redirect(string $value)

Example:

MoonShineJsonResponse::make()->redirect('/');

Events

The events() method adds JSEvents to the response, which will be triggered after processing an asynchronous request.

events(array $events)

Example:

MoonShineJsonResponse::make()->events([AlpineJs::event(JsEvent::TABLE_UPDATED, 'index')]);

Html

The html() method inserts the required HTML code into the selector specified when creating the component that initiated the request.

html(string|array $value, HtmlMode $mode = HtmlMode::INNER_HTML)
  • $value - the value to be inserted into the selector,
  • $mode - the mode of content replacement in the selector.

HtmlMode is an Enum with the following values:

enum HtmlMode: string
{
case INNER_HTML = 'inner_html';
 
case OUTER_HTML = 'outer_html';
 
case BEFORE_BEGIN = 'beforebegin';
 
case AFTER_BEGIN = 'afterbegin';
 
case BEFORE_END = 'beforeend';
 
case AFTER_END = 'afterend';
}

In the following example, the value Content will be inserted into the selector #my-selector

ActionButton::make('Button Label', '/endpoint')->async(selector: '#my-selector')
 
//...
 
MoonShineJsonResponse::make()->html('Content');

HtmlData

The htmlData() method allows specifying multiple selectors and HTML content for insertion into these selectors.

htmlData(string|array $value, string $selector, HtmlMode $mode = HtmlMode::INNER_HTML)

Example:

MoonShineJsonResponse::make()
->htmlData((string) Text::make('One'), '#selector1')
->htmlData((string) Text::make('Two'), '#selector2', HtmlMode::BEFORE_END)