Home > Mobile >  Is it possible to export a single data without exporting all the data present in the db?
Is it possible to export a single data without exporting all the data present in the db?

Time:10-07

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 \Illuminate\Support\Collection
    */
    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  href="{{ route('client.export') }}">Export all clients</a>

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

<a  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');   
    }

CodePudding user response:

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

CodePudding user response:

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 \Illuminate\Support\Collection
    */
    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');            
        
    }

CodePudding user response:

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.

  • Related