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

424
Views
laravel | foreach elocuente no funciona

Estoy tratando de recuperar todos los usuarios que no son administradores de mi tabla de usuarios. al igual que

 $agents = User::where('is_admin','=', 'false')->get(); //didn't work foreach ($agents as $agent=>$value) { echo "{$agent}=>{$value}"."<br>"; } //tried dumping dd($agents);

pero no funcionó, así que intenté volcar la variable para verificar si tenía algún resultado, tengo uno que no es administrador a partir de ahora: y aquí está el resultado

 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" } ] }

Por favor ayuda

about 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>"; }

Al usar arrancar, convertirá el objeto en una matriz, por lo que puede usar el foreach de la manera que desee con la key => value .

Si necesita acceder a otros atributos del modelo, deberá hacer algo como esto:

 $agents = User::where('is_admin','=', 'false')->get(); foreach ($agents as $agent) { echo "{$agent->id}=>{$agent->name}"."<br>"; }

De esta manera, solo necesita usar $agent y -> seguido del atributo que desea.

about 4 years ago · Santiago Trujillo Report

0

Su controlador debe ser como:

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

Y luego, en ese archivo de vista, haga el bucle foreach en la vista de hoja

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

0

Aquí $agents es una Std Class object array . así que acceda a sus elementos, intente esto:

 foreach ($agents as $agent) { $agent->name; }
about 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!