Just derive a row number field. I am guessing it is just for looks. But you don't need to show the id field anyway, just skip that field in a foreach, can still be part of the query.
Row num instead of db ID ?
Hi, is it possible to somehow show row num instead of ID field on the resources index page? My admin users don't really need to see real database id for each row, and I also don't want to reveal these ids to everyone. I could hide it, but then table feels empty, so it would be really nice to just show simple row numbers (from 1 to num of rows) on each table. Any suggestions how could I achieve this?
In case someone needs this, I've managed to solve this by using MySql's query session variables feature explained here.
First you have to override indexQuery method like this:
...
use Illuminate\Support\Facades\DB;
...
public static function indexQuery(NovaRequest $request, $query)
{
// Needed for proper row nums when using pagination
$offset = $request->input('perPage', 0) * ($request->input('page', 1) - 1);
DB::statement(DB::raw('set @row_number = 0'));
return $query->select([
DB::raw("(@row_number:=@row_number + 1) + $offset AS num"),
'your_table_name.*'
]);
}
Then, define 'rownum' field in resource $fields array:
Text::make('#', 'num')->sortable()
That's it.
Please or to participate in this conversation.