In the previous article “Laravel: Installing maatwebsite/excel and Exporting Excel Files“, we exported data from the users table directly using get and output it to Excel.
This time, we will introduce how to export search results (POST data) from the users table to Excel.
(Laravel version 6.18.39)
- Prepare the users table for the search
- Create a class to output all contents of the users table to the view
- View description for the Excel file output (/exports/excelusers.blade.php file)
- Controller description to output the Excel file (users.xlsx)
- Excel output URL (/routes/web.php) description
- View description for the search input screen (serchuser.blade.php file)
- Summary
Prepare the users table for the search
We have prepared the following users table. We will perform a like search on the data in the name (user name) column from the users table.
users table
Name | Description |
---|---|
id | Primary key |
name | User name |
Email address |
Create a class to output all contents of the users table to the view
We will create the following class (/app/Exports/Excelusers.php file) to output the search results from the users table to the view (exports/excelusers.blade.php).
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Contracts\View\View;
class Excelusers implements FromView
{
protected $users;
function __construct($users) {
$this->users = $users;
}
public function view(): View
{
// Add the following. Specify the root to the view in "excel.xxxxx"
return view('exports.excelusers', [
'users' => $this->users,
]);
}
}
?>
View description for the Excel file output (/exports/excelusers.blade.php file)
The view (/resources/views/exports/excelusers.blade.php file) that describes the contents to be output to the Excel file is as follows. We will output “name” and “email” to Excel.
<p>users table</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
Controller description to output the Excel file (users.xlsx)
The Controller (/app/Http/Controllers/ExcelusersController.php file) that outputs the search results from the name column of the users table to the Excel file (users.xlsx) is as follows.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Exports\Excelusers;
use Maatwebsite\Excel\Facades\Excel;
class ExcelusersController extends Controller
{
// Export search results to Excel
public function excelresult(Request $request)
{
$sql = '1=1';
$username = "";
if(!empty($request->username)){
$username = $request->username;
$sql .= " AND users.name like '%".$username."%'"; // Like search on the name column of the users table
}
$users = DB::select("SELECT * FROM users WHERE ".$sql." ORDER BY id DESC");
return Excel::download(new Excelusers($users), 'users.xlsx');
}
}
Excel output URL (/routes/web.php) description
Write the Excel (users.xlsx) output URL (post) in /routes/web.php.
<?php
Route::post('/excelusers', 'ExcelusersController@excelresult');
?>
View description for the search input screen (serchuser.blade.php file)
The view (/resources/views/serchuser.blade.php file) for the input screen to search the name column of the users table is as follows.
(The model (class), Controller, and web.php description for the search input screen are omitted)
<form enctype="multipart/form-data" action="{{ url('excelusers') }}" method="POST" class="form-horizontal">
@csrf
<div>Please enter the name registered in the users table that you want to output to Excel</div>
<input type="text" name="username" value="">
<div>
<button type="submit" class="btn btn-primary">
Output Search Results
</button>
</div>
</form>
*When you post the data from the input screen to “/usersexport” described in web.php, the search results of the name column in the users table will be output to “users.xlsx”.
Summary
Exporting search results (POST data) to Excel format in Laravel is a convenient feature that allows you to flexibly save and share data according to user conditions.
In this article, we used `maatwebsite/excel` to receive the input from a search form, extract only the relevant data from the users table, and download it as an Excel file (users.xlsx).
In particular, you can smoothly implement it by keeping the following points in mind:
- Create a form to receive POST requests
- Generate SQL queries based on search conditions
- Create an export class to pass the search results
- Prepare a view for Excel output
- Use Excel::download to process file output
By applying this method, you can improve work efficiency in various scenarios, such as report output in admin panels, saving user search results, etc. Try incorporating it into your own project.
*Use this as a reference at your own risk.