Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

438
Views
laravel | eloquent foreach not working

I am trying to retrieve all non admin users from my user table. like so

$agents = User::where('is_admin','=', 'false')->get();

//didn't work
foreach ($agents as $agent=>$value) 
{
    echo "{$agent}=>{$value}"."<br>";
}

//tried dumping
dd($agents);

but It didn't work so I tried dumping the variable to check if it had any results, I have one non-admin as of now: and here is the output

Collection {#219 ▼
#items: array:1 [▼
    0 => User {#222 ▼
      #casts: array:1 [▶]
      #fillable: array:6 [▶]
      #hidden: array:2 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▶]
      #original: array:10 [▶]
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
  ]
}

Please Help

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

$agents = User::where('is_admin','=', 'false')->pluck('value', 'agent'); 

foreach ($agents as $agent=>$value) {
echo "{$agent}=>{$value}"."<br>";
}

Using pluck you will convert the object to a array, so you can use the foreach the way you want with the key => value.

If you need to access other attributes of the model, you will need to do something like this:

$agents = User::where('is_admin','=', 'false')->get(); 

foreach ($agents as $agent) {
echo "{$agent->id}=>{$agent->name}"."<br>";
}

This way you just need to use $agent and -> followed by the attribute you want.

over 4 years ago · Santiago Trujillo Report

0

Your controller should be like:

public function index() {

    $agents = User::where('is_admin','=', 'false')->get();
    return view('viewfile', compact('agents'));
}

And then in that view file make the foreach loop in blade view

@foreach ($agents as $agent)
     {{ $agent->name }}
@endforeach
over 4 years ago · Santiago Trujillo Report

0

Here $agents is a Std Class object array. so access it elements try this:

foreach ($agents as $agent) 
{
    $agent->name;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!