To simplify the process, you can use the whereColumn method in Laravel's query builder to compare two fields in a record. Here's an example of how you can modify the code to achieve this:
$staff = DB::table('users')
->whereColumn('email', '=', 'email2')
->get();
foreach ($staff as $own) {
$this->ownSuperNames[] = [
'user_id' => $own->id,
'user_name' => $own->name,
'email' => $own->email,
'email2' => $own->email2,
];
}
The whereColumn method allows you to compare two columns in a table. In this case, it compares the email column with the email2 column. If they are equal, the record is included in the result set.
This approach eliminates the need for the if condition inside the loop, making the code more concise and efficient.