Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kokurate's avatar

Undefined variable $users

I'm currently using laravel 8 , i want to make sending email with condition but the $users is not undefined here's the code

public function email_officer(Complaint $complaint, Request $request){
    if($complaint->destination_id == 1){$users = User::where('level', 'network')->get();}
    elseif($complaint->destination_id == 2){$users = User::where('level', 'server')->get();}
    elseif($complaint->destination_id == 3){$users = User::where('level', 'lms')->get();}
						
// Message to email
    $data = [
      'content' => 'incoming complaint ,
      'url' => 'http://127.0.0.1:8000/login',
      ];

    // Loop
    foreach($users as $user){
    Mail::to($user->email)->send(new SendMailOfficer($data));
     }

     return back()->with('toast_success','Sending Email Succesfully.');
   }

before i use condition this code is working properly here's my previous code

public function email_officer(Complaint $complaint, Request $request){
 $users = User::where('level', 'network')->get();
						
// Message to email
    $data = [
      'content' => 'incoming complaint ,
      'url' => 'http://127.0.0.1:8000/login',
      ];

    // Loop
    foreach($users as $user){
    Mail::to($user->email)->send(new SendMailOfficer($data));
     }

     return back()->with('toast_success','Sending Email Succesfully.');
   }

can anyone help me to solve this as soon as posible thank you in advanced

0 likes
32 replies
Sinnbeck's avatar

If none of your 3 if statements are hit, $users are newer set. Just set a default before

$users = [];

if($complaint->destination_id == 1){$users = User::where('level', 'network')->get();}
    elseif($pengaduan->tujuan_id == 2){$users = User::where('level', 'server')->get();}
    elseif($pengaduan->tujuan_id == 3){$users = User::where('level', 'lms')->get();}
1 like
Sinnbeck's avatar

@kokurate exactly. You have no users as none of these 3 if statements are triggered.

Which one are you assuming is triggered and why?

Sinnbeck's avatar

Also where does $pengaduan come from? It's never set?

kokurate's avatar

@Sinnbeck Owh okay.

every condition that exists it will send an email because the email to be sent is based on the level. So if the level is network, then all network officers will receive this email

kokurate's avatar

@Sinnbeck it's actually a $complaint, i just translate that to english but i forgot to change that, so $pengaduan is $complaint

Sinnbeck's avatar

@kokurate ah ok. Well check the content of the variable and post it se we can better help

dd($pengaduan);
Snapey's avatar

we can't know what is in your database

kokurate's avatar

@Snapey in my database i'm already make the relationship and the field and value are exist when i use the 'where'

Snapey's avatar

@kokurate We can't know if your complaint destination is =1,2 or 3, or if you have any users of those types.

As @sinnbeck asked. What do you expect to happen when none of your checks match?

kokurate's avatar

@Snapey i have those users and when no check match it will show error message

MohamedTammam's avatar

@kokurate So make it throws an exception

if($complaint->destination_id == 1){$users = User::where('level', 'network')->get();}
elseif($complaint->tujuan_id == 2){$users = User::where('level', 'server')->get();}
elseif($complaint->tujuan_id == 3){$users = User::where('level', 'lms')->get();}	
else {
	throw new Exception('Got tujuan_id  with value ' . $complaint->destination_id)
}
lat4732's avatar

Make a request and show the output of dd($complaint) in order somebody to try to help you. I'm pretty sure your output will be null.

Sinnbeck's avatar

Just a wild guess. The problem comes because your route binding is named wrong

Route::get('someroute/{pengaduan}'... /this needs to match the name in the method 
kokurate's avatar

@Sinnbeck the route model binding is set up properly because before i make with if else condition. It works and also directly send the email to officer

kokurate's avatar

@Sinnbeck App\Models\Pengaduan {#1433 ▼ #guarded: array:1 [▶] #with: array:1 [▶] #connection: null #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #withCount: [] +preventsLazyLoading: false #perPage: 15 +exists: false +wasRecentlyCreated: false #escapeWhenCastingToString: false #attributes: [] #original: [] #changes: [] #casts: [] #classCastCache: [] #attributeCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #fillable: [] }

kokurate's avatar

anw how to make it look good? idk how to make it

Sinnbeck's avatar

@kokurate you can use ->toArray() on the model. And format here by adding ``` on the line before and after

Sinnbeck's avatar

@kokurate so it has no data. Can you show the actual code and the route? Without replacing variable names. It's fine with Indonesian

kokurate's avatar

@Sinnbeck Here's the rute when i clicked the button in view

   route::post('/admin/email-to-petugas',[AdminController::class,'email_petugas'])->name('email.petugas');

the actual code is in my comment below

Sinnbeck's avatar

@kokurate you are allowed to ask as many as you need. Great that it's working now :)

1 like
kokurate's avatar

I'm updated my code without translate it to english. Take a look

 public function email_petugas(Pengaduan $pengaduan, Request $request){

     $users = [];
    if($pengaduan->tujuan_id == 1){$users = User::where('level', 'jaringan')->get();}
    elseif($pengaduan->tujuan_id == 2){$users = User::where('level', 'server')->get();}
      elseif($pengaduan->tujuan_id == 3){$users = User::where('level', 'lms')->get();}
    else {
      return back()->with('toast_error','Email petugas tidak ada');
    }    

     // Message to email
     $data = [
      'content' => 'Ada pengaduan yang masuk' ,
      'url' => 'http://127.0.0.1:8000/login',
    ];

   // Loop 
     foreach($users as $user){
     Mail::to($user->email)->send(new SendMailOfficer($data));
   }

    return back()->with('toast_success','Berhasil Kirim Email ke petugas.');
   }

Please or to participate in this conversation.