php - laravel | eloquent foreach not working -
i trying retrieve non admin users user table.
$agents = user::where('is_admin','=', 'false')->get(); //didn't work foreach ($agents $agent=>$value) { echo "{$agent}=>{$value}"."<br>"; } //tried dumping dd($agents);
but didn't work tried dumping variable check if had results, have 1 non-admin of now: , here 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
$agents = user::where('is_admin','=', 'false')->pluck('value', 'agent'); foreach ($agents $agent=>$value) { echo "{$agent}=>{$value}"."<br>"; }
using pluck convert object array, can use foreach way want key => value
.
if need access other attributes of model, need this:
$agents = user::where('is_admin','=', 'false')->get(); foreach ($agents $agent) { echo "{$agent->id}=>{$agent->name}"."<br>"; }
this way need use $agent
, ->
followed attribute want.
Comments
Post a Comment