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)toast(string $value, ToastType $type = ToastType::DEFAULT, null|int|false $duration = null)
Example:
MoonShineJsonResponse::make()->toast('My message', ToastType::SUCCESS, duration: 3000);MoonShineJsonResponse::make()->toast('My message', ToastType::SUCCESS, duration: 3000);
Redirect
The redirect() method will redirect to the specified URL.
redirect(string $value)redirect(string $value)
Example:
MoonShineJsonResponse::make()->redirect('/');MoonShineJsonResponse::make()->redirect('/');
Events
The events() method adds JSEvents to the response, which will be triggered after processing an asynchronous request.
events(array $events)events(array $events)
Example:
MoonShineJsonResponse::make()->events([AlpineJs::event(JsEvent::TABLE_UPDATED, 'index')]);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)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';}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');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)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)MoonShineJsonResponse::make()->htmlData((string) Text::make('One'), '#selector1')->htmlData((string) Text::make('Two'), '#selector2', HtmlMode::BEFORE_END)
Fields values
The fieldsValues() method allows you to set the values of the field fields through selectors.
fieldsValues(array $values)fieldsValues(array $values)
Example:
MoonShineJsonResponse::make()->fieldsValues(['.field-title-1' => 'some value 1','.field-title-2' => 'some value 2',])MoonShineJsonResponse::make()->fieldsValues(['.field-title-1' => 'some value 1','.field-title-2' => 'some value 2',])
Also, when filling the field, the event change will be caused