I am using laravel excel to export data from 'User' table. A query needs to be run and all the records that match the query will return.
My code is like
class UsersExport implements FromQuery
{
use Exportable;
public function __construct(string $code)
{
$this->code = $code;
}
public function query()
{
return Tantousya::query()->where('code','>=',$this->code);
}
}
It is working perfectly and giving me correct data.
However, in the User table there is a "password" column. The clients want me to show strings like "*****" instead of showing the encrypted password. If no password is set then show ""(a blank string).
To illustrate it better, my current output is like,
How can I change my query function in such a way that I get my desired output?
I think you should try something like this:
public function query()
{
return Tantousya::query()->select(['*', \DB::raw("'***' as password")])->where('code','>=',$this->code);
}
The second way is to implement FromCollection then use the map method to change values.
Something like this:
use Maatwebsite\Excel\Concerns\FromCollection;
class UserExport implements FromCollection {
public function collection()
{
return User::select(['*'])
->get()
->map(function($item) {
$collection = $item;
$collection['password'] = '***';
return $collection;
});
}
}