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
Can you show the one that works?
@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
@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;
}
@usertxr that does not look like the full class?
@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');
}
@Sinnbeck Sorry i dont understant how to pass data throungh construct from my searchController.
@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;
}
}
Quick tip
if ($table->count() !== 0) {
//could just be
if ($table->isNotEmpty()) {
Please sign in or create an account to participate in this conversation.