Implementing sorting (changing the order of priority)

Add a sort order (sort_order) column to the database, and integrate SortableJS with htmx (hx-post) to save the sorted results.

1. Creating a migration file

Add a sort_order column (integer type) to the existing table to preserve the sort order.

Execute command:

Bash

copy
画像
Execution results

Pausing work to ask about a suspicious command: an explanation of [php artisan make:migration add_sort_order_to_test_tutorial_posts_table]

Please explain the relationship between ‘add_sort_order_to_’ and ‘test_tutorial_posts_table’ in the command ‘php artisan make:migration add_sort_order_to_test_tutorial_posts_table’.

Interview

This is an explanation of the structure and relationships within the filename string used in the command php artisan make:migration add_sort_order_to_test_tutorial_posts_table.

How the ‘snake case’ naming convention works

Laravel parses the string entered in the command line using snake case (a format where words are joined by underscores _) and automatically determines the internal processing.

  • add_sort_order_to_ (Prefixes indicating actions and content)

    • Role: This section describes the ‘intent (behaviour)’ of the migration process.

    • Meaning: This follows the rule ‘add_[name of the column to be added]_to_’, indicating the intention to ‘add the sort_order column’.

  • test_tutorial_posts (Name of the relevant table)

    • Role: The name of the table in the database on which operations are to be performed.

    • Relationship: Laravel automatically recognises the section between the ‘to_’ at the start and the ‘_table’ at the end as the ‘table name to which the changes should be applied’.

  • _table (Suffix indicating the type of migration)

    • Role: A keyword that explicitly indicates that this is a migration file relating to table operations.

Code automatically generated by Laravel as a result of this naming convention

If you run the command in accordance with the naming conventions outlined above, Laravel will interpret this as ‘modifying an existing table’ rather than ‘creating a new table’, and will generate the initial code for the migration file as follows.

PHP

copy

If you run a command using a name that does not conform to the rules (e.g. change_something), Schema::table(...) will not be generated automatically, and you will need to specify the target table name manually. The combination of add_~_to_ and ~_table is the definitive syntax for correctly informing Laravel of the target table.

I reckon it would be fine to just prepare a template and then copy, paste and rename.

Muttering to myself

Resumption of work


Edit file: database/migrations/xxxx_xx_xx_xxxxxx_add_sort_order_to_test_tutorial_posts_table.php
PHP

copy

Running the migration:

Bash

copy
画像
Execution results
画像
sort_orderカラムが追加された

2. Routing settings (routes/web.php)

Add the following to the ‘use’ section at the start of the file to configure the routes for display and for sorting and saving.

PHP

copy

3. Implementing the controller (app/Http/Controllers/TestTutorialPostController.php)

Retrieve the data in ascending order (sort_order) and update it using the POST data received.

PHP

copy

4. Implementing the Blade view

Full-screen view: resources/views/test_tutorial_post/reorder_index.blade.php

Use @extends or @section in the outer block to loop through and display lists using standard PHP syntax.

HTML

copy

Procedure for Verifying Operation

  1. Open your browser and go to /test-tutorial/posts/reorder (e.g. http://localhost/mutunet/public/test-tutorial/posts/reorder).

  2. Drag and drop the cards with your mouse to reorder them.

  3. The moment the item is dropped, hx-trigger="end" is triggered, and the message ‘Sort order saved’ appears in green text at the bottom of the screen.

  4. Reload the page (F5) and check that the sort order has been retained.