Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

Can any Brother guide me in writing the filter function at UserManagementController in Laravel?


Can anyone guide me to capture requests at this index so I can do the filter function, for example, filtering from date to date for UserManagementController in Laravel ?

class UserManagementController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(UsersDataTable $dataTable)
{
return $dataTable->render('pages/apps.user-management.users.list');
}


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (2)


Hi Thắng Lê

The Metronic Laravel project uses the Yajra DataTable plugin for handling data tables. To implement a filter function, you can refer to the official documentation of Yajra DataTables for manual search functionality.

Here is the link to the documentation:
https://yajrabox.com/docs/laravel-datatables/10.0/manual-search

The documentation provides detailed instructions on how to implement manual search functionality, which you can adapt to create a date range filter for your `UserManagementController`.

If you need further assistance, feel free to ask!

Thanks



To capture requests at the index of your `UserManagementController` in Laravel so that you can implement filtering from date to date, you can follow these steps:
1. Define route for the index method:

```php
Route::get('/users', [UserManagementController::class, 'index'])->name('users.index');
```

2. Modify the `index` method in your `UserManagementController` to capture the request parameters for filtering:

```php
use Illuminate\Http\Request;
class UserManagementController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request, UsersDataTable $dataTable)
{
$startDate = $request->input('start_date');
$endDate = $request->input('end_date');

// Pass the start date and end date to your data table
$dataTable->setStartDate($startDate);
$dataTable->setEndDate($endDate);

return $dataTable->render('pages.apps.user-management.users.list');
}
}
```
3. In your `UsersDataTable` class, implement the `setStartDate()` and `setEndDate()` methods to handle the filtering:

```php
class UsersDataTable extends DataTable
{
protected $startDate;
protected $endDate;

public function setStartDate($startDate)
{
$this->startDate = $startDate;
}

public function setEndDate($endDate)
{
$this->endDate = $endDate;
}

public function query()
{
$query = User::query();

// Apply date range filtering if start date and end date are provided
if ($this->startDate && $this->endDate) {
$query->whereBetween('created_at', [$this->startDate, $this->endDate]);
}

return $query;
}
}
```
4. In your view file `pages/apps.user-management.users.list`, add date input fields for users to enter the start date and end date for filtering.

```html
<form method="GET" action="{{ route('users.index') }}">
<label for="start_date">Start Date:</label>
<input type="date" name="start_date">

<label for="end_date">End Date:</label>
<input type="date" name="end_date">

<button type="submit">Filter</button>
</form>
```
With these changes, users can now enter the start date and end date in the form, and the `index` method in your `UserManagementController` will capture these requests and pass them to your `UsersDataTable` for filtering the enneagram test data accordingly.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(