Laravel Livewire Tables Documentation

🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.

This is the documentation for v3. You can switch versions in the menu at the top. Check your current version with the following command:

composer show rappasoft/laravel-livewire-tables

Saving

When you click the "Save" button, your ordered elements will be returned as an array, to the function you have configured.

You will receive a multi-dimensional array, containing an array item per record, with the primary key, and order field.

It is recommended that you perform some validation before bulk updating data, but it is in the correct format to perform upserts.

1public function reorder(array $items): void
2{
3 // $item[$this->getPrimaryKey()] ensures that the Primary Key is used to find the User
4 // 'sort' is the name of your "sort" field in your database
5 // $item[$this->getDefaultReorderColumn()] retrieves the field, as defined in setDefaultReorderSort('FIELD', 'ORDER')
6 
7 foreach ($items as $item) {
8 User::find($item[$this->getPrimaryKey()])->update(['sort' => (int)$item[$this->getDefaultReorderColumn()]]);
9 }
10}

If you have defined both the Primary Key, and the Order Column, and the fields match your database, then you could use an upsert(). Keep in mind that an upsert does not go through validation, and you should exercise caution with this approach

1public function reorder(array $items): void
2{
3 // First value is the array of items
4 // Second value should be the unique id (set in setPrimaryKey())
5 // Third value should be the field set in setDefaultReorderSort('FIELD', 'ORDER')
6 User::upsert($items, [$this->getPrimaryKey()], [$this->getDefaultReorderColumn()]);
7}