- Joined
- Nov 19, 2020
- Posts
- 2,684
- Reaction
- 4,446
- Points
- 1,548
How to make a filter by date in a data table using laravel?
whereDate
method provided by the Query Builder. Here's an example how you can use it:$startDate = '2021-01-01';
$endDate = '2021-12-31';
$data = DB::table('table_name')
->whereDate('created_at', '>=', $startDate)
->whereDate('created_at', '<=', $endDate)
->get();
table_name
where the created_at
column is between the $startDate
and $endDate
. You can change the column name and date range based on your requirements.DB
facade at the top of your file like this:use Illuminate\Support\Facades\DB;
How to add a filter by date button that filters from the start date to end date using laravel?To filter data by date in a data table using Laravel, you can use thewhereDate
method provided by the Query Builder. Here's an example how you can use it:
PHP:$startDate = '2021-01-01'; $endDate = '2021-12-31'; $data = DB::table('table_name') ->whereDate('created_at', '>=', $startDate) ->whereDate('created_at', '<=', $endDate) ->get();
In this example, we are getting all the data fromtable_name
where thecreated_at
column is between the$startDate
and$endDate
. You can change the column name and date range based on your requirements.
Also, make sure to import theDB
facade at the top of your file like this:
PHP:use Illuminate\Support\Facades\DB;
I hope this helps!
To add a filter by date button that filters data in a data table from a start date to an end date using Laravel, you can follow these steps:How to add a filter by date button that filters from the start date to end date using laravel?
<form action="{{ url('/filter-data') }}" method="get">
<div class="form-group">
<label for="start_date">Start Date:</label>
<input type="date" name="start_date" class="form-control" id="start_date">
</div>
<div class="form-group">
<label for="end_date">End Date:</label>
<input type="date" name="end_date" class="form-control" id="end_date">
</div>
<button type="submit" class="btn btn-primary">Filter Data</button>
</form>
Route::get('/filter-data', function (Request $request) {
$startDate = $request->input('start_date');
$endDate = $request->input('end_date');
$data = DB::table('table_name')
->whereBetween('created_at', [$startDate, $endDate])
->get();
return view('data-table', ['data' => $data]);
})->name('filter-data');
<a href="{{ route('filter-data') }}" class="btn btn-primary">Filter Data</a>
whereBetween
method to filter the data between the start and end date. You can customize the query based on your requirements.To add a filter by date button that filters data in a data table from a start date to an end date using Laravel, you can follow these steps:
1. Create a form that allows the user to input the start and end date:
HTML:<form action="{{ url('/filter-data') }}" method="get"> <div class="form-group"> <label for="start_date">Start Date:</label> <input type="date" name="start_date" class="form-control" id="start_date"> </div> <div class="form-group"> <label for="end_date">End Date:</label> <input type="date" name="end_date" class="form-control" id="end_date"> </div> <button type="submit" class="btn btn-primary">Filter Data</button> </form>
2. Create a route that handles the form submission:
PHP:Route::get('/filter-data', function (Request $request) { $startDate = $request->input('start_date'); $endDate = $request->input('end_date'); $data = DB::table('table_name') ->whereBetween('created_at', [$startDate, $endDate]) ->get(); return view('data-table', ['data' => $data]); })->name('filter-data');
3. In the controller or view that displays the data table, create a link to the filter form:
HTML:<a href="{{ route('filter-data') }}" class="btn btn-primary">Filter Data</a>
When the user clicks the "Filter Data" button, they will be taken to the form where they can input the start and end date, and then click the "Filter Data" button again to filter the data table. The filtered data will be displayed in the same view where the data table is displayed.
In this example, we used thewhereBetween
method to filter the data between the start and end date. You can customize the query based on your requirements.
npm install sweetalert --save
<link href="{{ asset('css/sweetalert.css') }}" rel="stylesheet">
<script src="{{ asset('js/sweetalert.min.js') }}"></script>
<script>
$(document).on('click', '#save-button', function (e) {
e.preventDefault(); // prevent form submission
var form = $('form'); // get the form element
swal({
title: "Are you sure?",
text: "You want to update this row?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, update it!",
cancelButtonText: "No, cancel please!",
closeOnConfirm: false,
closeOnCancel: false
}, function (isConfirm) {
if (isConfirm) {
form.submit(); // submit the form if user clicks on confirm button
} else {
swal("Cancelled", "Row update is cancelled", "error");
}
});
});
</script>
<button type="submit" id="save-button" class="btn btn-primary">Save</button>
To display a confirm message after clicking the save button for updating a table row using sweet alert in Laravel, you can follow the below steps:
Step 1: First, you need to install Sweet Alert using the following command:
Code:npm install sweetalert --save
Step 2: After installing Sweet Alert, you need to include its CSS and JS files in your project. You can do this by adding the following lines of code in the head section of your blade file:
Code:<link href="{{ asset('css/sweetalert.css') }}" rel="stylesheet"> <script src="{{ asset('js/sweetalert.min.js') }}"></script>
Step 3: Next, you need to add an event listener to the save button in your blade file. You can do this by adding the following code:
Code:<script> $(document).on('click', '#save-button', function (e) { e.preventDefault(); // prevent form submission var form = $('form'); // get the form element swal({ title: "Are you sure?", text: "You want to update this row?", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes, update it!", cancelButtonText: "No, cancel please!", closeOnConfirm: false, closeOnCancel: false }, function (isConfirm) { if (isConfirm) { form.submit(); // submit the form if user clicks on confirm button } else { swal("Cancelled", "Row update is cancelled", "error"); } }); }); </script>
In the above code, we have added an event listener to the save button. When the user clicks on the save button, it will prevent the form submission and display a confirmation message using Sweet Alert. If the user clicks on the confirm button, the form will be submitted and the row will be updated. If the user clicks on the cancel button, a "Cancelled" message will be displayed.
Step 4: Finally, you need to add an ID to the save button in your blade file. You can do this by adding the following code:
Code:<button type="submit" id="save-button" class="btn btn-primary">Save</button>
Now, when the user clicks on the save button, a confirmation message will be displayed using Sweet Alert. If the user clicks on the confirm button, the form will be submitted and the row will be updated. If the user clicks on the cancel button, the row update will be cancelled.
In Laravel, you can restore a soft deleted row by using theHow to restore a soft deleted row using laravel?
restore
method provided by the Eloquent ORM. To do this, you need to follow these steps:deleted_at
column. This column is used by Laravel to store the timestamp of when a row was deleted.SoftDeletes
trait in your model. This trait provides the softDeletes
method which makes your model use soft delete functionality. Here's an example:use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class YourModel extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
withTrashed
method. This method returns all rows including the soft deleted ones. Here's an example:public function index()
{
$rows = YourModel::withTrashed()->get();
return view('your-view', compact('rows'));
}
@foreach($rows as $row)
<tr>
<td>{{ $row->id }}</td>
<td>{{ $row->name }}</td>
<td>{{ $row->description }}</td>
<td>{{ $row->deleted_at ? 'Deleted' : 'Active' }}</td>
<td>
@if($row->deleted_at)
<form action="{{ route('your-route.restore', $row->id) }}" method="POST">
@csrf
@method('PUT')
<button type="submit">Restore</button>
</form>
@endif
</td>
</tr>
@endforeach
Route::put('/your-route/{id}/restore', 'YourController@restore')->name('your-route.restore');
public function restore($id)
{
YourModel::withTrashed()->where('id', $id)->restore();
return redirect()->back()->with('success', 'Row restored successfully.');
}
{id}
parameter and the restore
method in the YourController
. In the restore
method, we use the withTrashed
method to retrieve the soft deleted row with the specified ID and then call the restore
method on it to restore it. Finally, we redirect back to the previous page with a success message.