Skip to content

Commit 8f7c3b5

Browse files
committed
Add comprehensive usage examples for accent-insensitive search
- Add controller examples showing Eloquent, Collection, and Query Builder usage - Add Blade template example with search instructions - Add migration example with Portuguese test data - Add route and configuration examples - Demonstrates real-world usage scenarios
1 parent 2470514 commit 8f7c3b5

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?php
2+
3+
/**
4+
* Example usage of accent-insensitive search in Laravel DataTables
5+
*
6+
* This example shows how to use the accent-insensitive search feature
7+
* to handle Portuguese Brazilian accents.
8+
*/
9+
10+
namespace App\Http\Controllers;
11+
12+
use Illuminate\Http\Request;
13+
use DataTables;
14+
use App\Models\User;
15+
16+
class UserController extends Controller
17+
{
18+
/**
19+
* Example 1: Basic Eloquent DataTable with accent-insensitive search
20+
*/
21+
public function getUsersData(Request $request)
22+
{
23+
// Make sure ignore_accents is enabled in config/datatables.php:
24+
// 'search' => ['ignore_accents' => true]
25+
26+
return DataTables::of(User::query())
27+
->addColumn('action', function ($user) {
28+
return '<button class="btn btn-sm btn-primary">View</button>';
29+
})
30+
->rawColumns(['action'])
31+
->make(true);
32+
}
33+
34+
/**
35+
* Example 2: Collection DataTable with accent-insensitive search
36+
*/
37+
public function getBrazilianCitiesData()
38+
{
39+
$cities = collect([
40+
['id' => 1, 'name' => 'São Paulo', 'state' => 'SP'],
41+
['id' => 2, 'name' => 'João Pessoa', 'state' => 'PB'],
42+
['id' => 3, 'name' => 'Ribeirão Preto', 'state' => 'SP'],
43+
['id' => 4, 'name' => 'Florianópolis', 'state' => 'SC'],
44+
['id' => 5, 'name' => 'Maceió', 'state' => 'AL'],
45+
['id' => 6, 'name' => 'São Luís', 'state' => 'MA'],
46+
]);
47+
48+
return DataTables::of($cities)->make(true);
49+
}
50+
51+
/**
52+
* Example 3: Query Builder with accent-insensitive search
53+
*/
54+
public function getEmployeesData()
55+
{
56+
$query = DB::table('employees')
57+
->select(['id', 'name', 'department', 'position'])
58+
->where('active', true);
59+
60+
return DataTables::of($query)
61+
->addColumn('formatted_name', function ($employee) {
62+
return ucwords(strtolower($employee->name));
63+
})
64+
->make(true);
65+
}
66+
}
67+
68+
/**
69+
* Example Blade template for the DataTable
70+
*/
71+
?>
72+
73+
{{-- resources/views/users/index.blade.php --}}
74+
<!DOCTYPE html>
75+
<html>
76+
<head>
77+
<title>Users with Accent-Insensitive Search</title>
78+
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.css">
79+
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
80+
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
81+
</head>
82+
<body>
83+
<div class="container">
84+
<h1>Users - Accent-Insensitive Search Example</h1>
85+
86+
<p>Try searching for:</p>
87+
<ul>
88+
<li><strong>simoes</strong> to find "Simões"</li>
89+
<li><strong>joao</strong> to find "João"</li>
90+
<li><strong>sao paulo</strong> to find "São Paulo"</li>
91+
<li><strong>jose</strong> to find "José"</li>
92+
</ul>
93+
94+
<table id="users-table" class="display" style="width:100%">
95+
<thead>
96+
<tr>
97+
<th>ID</th>
98+
<th>Name</th>
99+
<th>Email</th>
100+
<th>City</th>
101+
<th>Action</th>
102+
</tr>
103+
</thead>
104+
</table>
105+
</div>
106+
107+
<script>
108+
$(document).ready(function() {
109+
$('#users-table').DataTable({
110+
processing: true,
111+
serverSide: true,
112+
ajax: '{!! route('users.data') !!}',
113+
columns: [
114+
{data: 'id', name: 'id'},
115+
{data: 'name', name: 'name'},
116+
{data: 'email', name: 'email'},
117+
{data: 'city', name: 'city'},
118+
{data: 'action', name: 'action', orderable: false, searchable: false},
119+
]
120+
});
121+
});
122+
</script>
123+
</body>
124+
</html>
125+
126+
<?php
127+
/**
128+
* Example Migration for creating test data with accented names
129+
*/
130+
131+
use Illuminate\Database\Migrations\Migration;
132+
use Illuminate\Database\Schema\Blueprint;
133+
use Illuminate\Support\Facades\Schema;
134+
use Illuminate\Support\Facades\DB;
135+
136+
return new class extends Migration
137+
{
138+
public function up()
139+
{
140+
Schema::create('users', function (Blueprint $table) {
141+
$table->id();
142+
$table->string('name');
143+
$table->string('email')->unique();
144+
$table->string('city');
145+
$table->timestamps();
146+
});
147+
148+
// Insert sample data with Portuguese accents
149+
DB::table('users')->insert([
150+
['name' => 'João Silva', 'email' => '[email protected]', 'city' => 'São Paulo'],
151+
['name' => 'María Santos', 'email' => '[email protected]', 'city' => 'Rio de Janeiro'],
152+
['name' => 'José Oliveira', 'email' => '[email protected]', 'city' => 'Belo Horizonte'],
153+
['name' => 'Ana Conceição', 'email' => '[email protected]', 'city' => 'Salvador'],
154+
['name' => 'Paulo Ribeirão', 'email' => '[email protected]', 'city' => 'Ribeirão Preto'],
155+
['name' => 'Tatiane Simões', 'email' => '[email protected]', 'city' => 'João Pessoa'],
156+
['name' => 'Carlos São', 'email' => '[email protected]', 'city' => 'São Luís'],
157+
]);
158+
}
159+
160+
public function down()
161+
{
162+
Schema::dropIfExists('users');
163+
}
164+
};
165+
166+
/**
167+
* Example Routes
168+
*/
169+
170+
// routes/web.php
171+
Route::get('/users', [UserController::class, 'index'])->name('users.index');
172+
Route::get('/users/data', [UserController::class, 'getUsersData'])->name('users.data');
173+
Route::get('/cities/data', [UserController::class, 'getBrazilianCitiesData'])->name('cities.data');
174+
175+
/**
176+
* Configuration Example
177+
*/
178+
179+
// config/datatables.php
180+
return [
181+
'search' => [
182+
'smart' => true,
183+
'multi_term' => true,
184+
'case_insensitive' => true,
185+
'use_wildcards' => false,
186+
'starts_with' => false,
187+
'ignore_accents' => true, // <-- Enable accent-insensitive search
188+
],
189+
// ... rest of configuration
190+
];

0 commit comments

Comments
 (0)