Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

Export search result as excel format

I need to export results from this search on Excel format

this is search function

 public function advance(Request $request)
{
    $table = \DB::table('table_1');
    if( $request->dtcreated){
        $table = $table->where('created_at', 'LIKE', "%" . $request->dtcreated . "%");
    }
    if( $request->userSearch){
        $table = $table->where('user_id', 'LIKE', '%' . $request->userSearch . '%');
    }
    
    $table = $table->paginate(10); 

    if ($table->count() !== 0) {
        return view('search', compact('table' , 'tetrixsCount')); 
    }
        return redirect()->back()->withErrors(['Oops! Nothing Found']); 
 }

I have an export function on another controller but from there i download all data from db. And now i need to download just searched results from this search function

0 likes
11 replies
Maison012's avatar

@Sinnbeck This works but save all data from table

public function exportIntoExcel() {
    return Excel::download(new TableExport, 'TableExport.xlsx');
}
public function exportIntoCSV() {
    return Excel::download(new TableExport, 'TableExport.csv');
}

and this is route

Route::get('/export-excel', [MemberController::class,'exportIntoExcel']);
Route::get('/export-csv', [MemberController::class, 'exportIntoCSV']);

now i want to download just searched record based in function i have writed on searchController

Maison012's avatar

@Sinnbeck i use this on modal TableExport

public static function getTable(){
    $records = DB::table('table_1')->select('id', 'user_id', 'created_at',)->get()->toArray();
    return $records;
  }
Maison012's avatar

@Sinnbeck I select the column from table on TableExport modal and i have done this function on member controller

public function exportIntoExcel() {
return Excel::download(new TableExport, 'TableExport.xlsx');
}
 public function exportIntoCSV() {
return Excel::download(new TableExport, 'TableExport.csv');
}
Maison012's avatar

@Sinnbeck Sorry i dont understant how to pass data throungh construct from my searchController.

Sinnbeck's avatar

@usertxr something like this

$table = \DB::table('table_1');
    if( $request->dtcreated){
        $table = $table->where('created_at', 'LIKE', "%" . $request->dtcreated . "%");
    }
    if( $request->userSearch){
        $table = $table->where('user_id', 'LIKE', '%' . $request->userSearch . '%');
    }

return Excel::download(new CustomExport($table->get()->toArray()), 'TableExport.xlsx');

//class
namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;

class CustomExport implements FromArray
{
    protected $data;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function array(): array
    {
        return $this->data;
    }
} 
Sinnbeck's avatar

Quick tip

if ($table->count() !== 0) {
//could just be
if ($table->isNotEmpty()) {

Please or to participate in this conversation.