You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an Excel report which I receive from a third party that tells me that users in my system have access to another system. Essentially, in my application there's a button that says go to other system.
Anyway, I use Laravel Excel to import this file, then I loop through the rows to see if a user in my application has a matching email address and if they do, I update a field.
Below is my approach
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Imports\CustodianUsersImport;
use App\User;
use Illuminate\Http\Request;
use Log;
class CustodianImportController extends Controller
{
/**
* Display a form so that users can select an Excel document to import.
*/
public function create()
{
return view('admin.users.consolidate');
}
/**
* Import data from an uploaded Excel document and loop through to see if we should allow custodian access or not.
*/
public function import(Request $request)
{
$importedEmails = [];
$request->validate([
'upload' => [
'required',
'file',
'mimes:xlsx',
'max:1000'
]
]);
$importData = (new CustodianUsersImport)->toArray($request->file('upload'));
foreach ($importData[0] as $row) {
$importedEmails[] = $row['email'];
}
$this->grantCustodianAccess($importedEmails);
return redirect()->back()->with('success', 'Consolodated users successfully');
}
/**
* Iterate over the given email addresses,
* if a user is found grant access to custodian portal.
*/
private function grantCustodianAccess(array $emails)
{
User::whereIn('email', $emails)
->where('has_custodian_access', 1)
->get()
->each(function ($user) {
$user->update('has_custodian_access', 1);
Log::info("{$user->full_name} was granted access to the MainSpring portal.");
});
$unconsolodatedUsers = User::select('email')->whereNotIn('email', $emails)->count();
Log::info("{$unconsolodatedUsers} investors were not found in the data import");
}
}
Should I be doing this logic within the import class itself?
I know I could use the ToCollection trait like so
namespace App\Imports;
use App\User;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class UsersImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
User::create([
'name' => $row[0],
]);
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have an Excel report which I receive from a third party that tells me that users in my system have access to another system. Essentially, in my application there's a button that says go to other system.
Anyway, I use Laravel Excel to import this file, then I loop through the rows to see if a user in my application has a matching email address and if they do, I update a field.
Below is my approach
Should I be doing this logic within the import class itself?
I know I could use the ToCollection trait like so
But instead within each loop I could do
With this though, I don't know if this would be an awful performance hit?
Anyone got a better implementation?
Beta Was this translation helpful? Give feedback.
All reactions