What is the result returned by your Excel::download(...)? Is a file created or a download response like with File Storage (https://laravel.com/docs/8.x/filesystem#downloading-files)?
Jan 18, 2021
11
Level 1
How to export the search result using Excel in laravel?
I can download all my data list but I want to export the results of my search can someone guide me how to do it? its my search controller
public function index(Request $request)
{
$dealers = Dealer::get();
$name = $request->get('name');
$email = $request->get('email');
$city = $request->get('city');
$mobile_no = $request->get('mobile_no');
$dealers = Dealer::query();
if ($name) {
$dealers = $dealers->where('name', 'LIKE', '%'.$name.'%');
}
if ($email) {
$dealers = $dealers->where('email', 'LIKE', '%'.$email.'%');
}
if ($city) {
$dealers = $dealers->where('city', 'LIKE', '%'.$city.'%');
}
if ($mobile_no) {
$dealers = $dealers->where('mobile_no', 'LIKE', '%'.$mobile_no.'%');
}
$dealers = $dealers->orderBy('id', 'DESC')->paginate(5);
if ($dealers->count() !== 0) {
return view('dealer.index', compact('dealers' ));
}
return redirect()->back()->withErrors(['Oops! Nothing Found']);
}
I tried to add this in my if condition part but its not working
if ($dealers->count() !== 0) {
$exportSearchResults = Excel::download(new DealersExport, 'dealer-search-results.xlsx');
return view('dealer.index', compact('dealers' , 'exportSearchResults'));
}
return redirect()->back()->withErrors(['Oops! Nothing Found']);
Please or to participate in this conversation.