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

tinkerbell's avatar

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']);

0 likes
11 replies
tinkerbell's avatar

actually Nothing!!! @nimrod

I have another button for downloading my data list

DealersExport.php


class DealersExport implements FromCollection, WithHeadings, WithMapping
{
    use Exportable;
    public function collection()
    {
      return Dealer::all();
    }

     public function map($row): array{
           $fields = [
              $row->id,
              $row->name,
              $row->email,
         ];
        return $fields;
    }

    public function headings(): array
        {
            return [
                'id',
                'name',
                'email',
            ];
    }
}

controller for DealersExport.php

    public function exportDealer() 
    {
        return Excel::download(new DealersExport, 'dealer-collection.xlsx');
    }     

It's working fine will print all my data of that table but I don't want to change it just I want to add another export for my searched data

pedroroccon's avatar

@faeza97 Hmmm. What do you mean with "Searched data"? It is the results of your search, or the parameters used in to search?

1 like
bhojkamal's avatar

@tinkerbell,

Please post your solution. I also need laravel export with where condition. But the the parameters should come from the browser/client side as parameter patient id. E.g. I want to export followup details for only specific patient from followups table, which contains all patients followup.

please post the solution you have. Regards.

Ariful Sikder's avatar

If i pass the search result on the session and set the session data inside the function of export, it will be work. but this method have a problem, suppose i wanna download without search, that time download prefer the previous session data😯😌

swamypatel's avatar

hi tinker if you have completed that task plz can you post that source code

Please or to participate in this conversation.