skip to Main Content

I am trying to export the data present in my database; in particular all the clients present. So far there are no problems.

Now I would like to export to single client; I tried to set the code only that when I click to export the single client in an .xlsx file all clients are exported the same.
Here is my code:

ClientsExport Class

class ClientsExport implements FromCollection
{
    /**
    * @return IlluminateSupportCollection
    */
    public function collection(Client $client=NULL)
    {
        return Client::all();
    }
}

ClientController

public function export(Client $client=NULL) 
    {
        if($client){
            return Excel::download(new ClientsExport, $client->surname . ' ' .  $client->name . '.xlsx');
        } else {
            return Excel::download(new ClientsExport, 'clients.xlsx');            
        }
    }

Routes

Route::get('client-export/{client?}', [ClientController::class,'export'])->name('client.export');

View Blade

Button where I want to export all clients (this works fine)

<a class="btn btn-warning mb-5 py-3 px-4 mt-3 me-3 fs-5" href="{{ route('client.export') }}">Export all clients</a>

Button where I want to export the individual client (PROBLEM HERE)

<a class="btn btn-warning" href="{{ route('client.export' , compact('client')) }}">Export</a> 

export fuction update:

 public function export(Client $client) 
    {
        if($client){
            dd($client);
            return Excel::download(new ClientsExport($client), $client->surname . ' ' .  $client->name . '.xlsx');
        } 
        return Excel::download(new ClientsExport, 'clients.xlsx');   
    }

3

Answers


  1. Create the property on the export. Make it able to assign it on creation. While making the collection dependent on if the client is present or not.

    class ClientsExport implements FromCollection
    {
        private $client = null;
    
        public function __construct($client = null)
        {
            $this->client = $client;
        }
    
        public function collection(Client $client=NULL)
        {
            if ($this->client) {
                return collect([$this->client]);
            }
    
            return Client::all();
        }
    }
    

    When you call it you can pass the client and it will on creation filter the collection based on the logic we just created.

    if ($client) {
        return Excel::download(new ClientsExport($client), $client->surname . ' ' .  $client->name . '.xlsx');
    }
            
    return Excel::download(new ClientsExport, 'clients.xlsx');            
    
    Login or Signup to reply.
  2. You are alwayse returning the same data from your ClientsExport Class, so you’re not even using the $client valiable, instead you are accessing the static class all() of your Client model.

    So to refactor this you need to use your injected client in your class

    class ClientsExport implements FromCollection
    {
    
        public function __construct ($client) 
        {
             $this->client = $client;
        }
        /**
        * @return IlluminateSupportCollection
        */
        public function collection()
        {
            return $this->client;
        }
    }
    

    then in your controller check if the Client is set or not and return appropriate Client instance

    public function export(Client $client) 
        {
            if($client->exists){
                return Excel::download(new ClientsExport($client),$client->surname . ' ' .  $client->name . '.xlsx');
            } 
    
            return Excel::download(new ClientsExport(User::all()), 'clients.xlsx');            
            
        }
    
    Login or Signup to reply.
  3. what is doing is first I get data from database of that particular user/client:

    In Controller:

    $data = CLIENT::where('id', $request->id)->get();
    return Excel::download(new ExportUsers($data), $data->client_name. '.xlsx');
    

    In Export class:

    private $data;
    
    public function __construct($data)
    {
        $this->data = $data;
    }
    
    public function collection(Client $client=NULL)
    {
        return Client::where('id',$this->data->id)->get();
    }
    

    Try this I hope it work for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search