Write a Laravel Blade component, containing an AlpineJS component that's an unordered list of checkboxes for a collection of line items with names and and prices. The line items are available in the $lineItems variable, and have properties for name, price, and formatted price. There should be a submit button underneath the list, whose value is "Refund X in FOH" where X is a live-updated sum of all the prices of only the checked items, formatted in GBP like "£9.99"
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is an example of how you can create a Laravel Blade component that incorporates an AlpineJS component to achieve the described functionality. This example assumes you have basic familiarity with both Laravel Blade and AlpineJS. If you haven't already, ensure AlpineJS is included in your project (typically in the <head> section of your layout file or where appropriate for your setup). AlpineJS can be added via CDN for simplicity:
Create a new Blade Component (e.g., RefundLineItemsComponent.php) in the app/View/Components directory:
php
// app/View/Components/RefundLineItemsComponent.php
namespace App\View\Components;
use Illuminate\View\Component;
class RefundLineItemsComponent extends Component
{
// The collection of line items passed from the controller
public $lineItems;
// Constructor to initialize the component with line items
public function __construct($lineItems)
{
$this->lineItems = $lineItems;
}
// Render the view for the component
public function render()
{
return view('components.refund-line-items');
}
}
Create the corresponding Blade view for the component (e.g., refund-line-items.blade.php) in the resources/views/components directory:
blade
<!-- resources/views/components/refund-line-items.blade.php -->
<!-- AlpineJS Component: Manages checkbox state and total calculation -->
<div x-data="refundSelector()">
<ul>
@foreach($lineItems as $item)
<li>
<!-- Checkbox for each line item -->
<input type="checkbox"
@click="updateTotal($event, {{ $item->price }})"
id="item-{{ $item->id }}"
value="{{ $item->price }}">
<!-- Label for the checkbox, including item name and formatted price -->
<label for="item-{{ $item->id }}">{{ $item->name }} ({{ $item->formatted_price }})</label>
</li>
@endforeach
</ul>
<!-- Submit button with dynamically updated text based on total -->
<button type="submit" x-text="'Refund ' + formattedTotal + ' in FOH'">
Refund £0.00 in FOH
</button>
</div>
<script>
// AlpineJS function to manage checkbox interactions and total calculation
function refundSelector() {
return {
// Initialize total and formattedTotal properties
total: 0,
formattedTotal: '£0.00',
// Function to update total when a checkbox is clicked
updateTotal(event, price) {
if (event.target.checked) {
this.total += price;
} else {
this.total -= price;
// Ensure total doesn't go below 0 if all checkboxes are unchecked
this.total = Math.max(this.total, 0);
}
// Format total as GBP (e.g., £9.99) with comma separation for thousands
this.formattedTotal = '£' + this.total.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
}
</script>
Explanation:
Component Class (RefundLineItemsComponent.php): This class accepts a collection of $lineItems in its constructor, making them available to the view.
Blade View (refund-line-items.blade.php):
AlpineJS Component (x-data="refundSelector()"): Initializes the AlpineJS component with the refundSelector function.
Checkbox List: Iterates over $lineItems, displaying each as a checkbox with its name and formatted price. The @click directive calls updateTotal when a checkbox is clicked, passing the event and the item's price.
Submit Button: Uses x-text to dynamically update the button's text with the current total, formatted as a GBP price.
JavaScript (refundSelector function):
Initializes total to 0 and formattedTotal to '£0.00'.
Defines updateTotal: Toggles the item's price in/out of the total, then updates formattedTotal to display the new sum in a GBP format (e.g., '£9.99'). The formatting includes comma separation for thousands and ensures the total does not go below 0 when unchecking items.
Ensure $yourLineItemsCollection is a collection of objects with id, name, price, and formatted_price properties, passed from your controller to the view. For example, in your controller:
php
public function yourMethod()
{
$lineItems = LineItem::all(); // Assuming LineItem model has the required properties
return view('your.view', compact('lineItems'));
}
Each LineItem object should have id, name, price, and formatted_price attributes. You can add an accessor to your LineItem model for formatted_price:
php
// In app/Models/LineItem.php
public function getFormattedPriceAttribute()
{
return '£' . number_format($this->price, 2);
}
This ensures that formatted_price is automatically available for each LineItem instance.