我正在尝试在Laravel中显示查询中的名称列表,我无法正确显示结果。
这是我在控制器中的内容:
public function getSettings()
{
$contents = explode(",", File::get(storage_path('app/public/users/0/config/social_module/buddy.txt')));
foreach($contents as $key => $value){
$buddylist[$value] = User::select('fullname as name')->where('id', $value)->get();
}
return view('welcome', compact('buddylist'));
}
以下是我的观点:
@foreach($buddylist as $buddy)
<ul>
<li>{{ $buddy }}</li>
</ul>
@endforeach
如果我使用$ buddy =&gt; $键,则不会显示任何内容。 如果我使用它,这就是结果:
[{"name":"Alix Earnshaw"}]
[{"name":"Horacio Esgate"}]
[{"name":"Patrizia Anthony"}]
[{"name":"Ann-marie McLewd"}]
[{"name":"Boote Poulsum"}]
[{"name":"Any Bandy"}]
[{"name":"Francene Lelliott"}]
[{"name":"Ingelbert Abdy"}]
[{"name":"Nickolaus Di Biaggi"}]
[{"name":"Constantina Wreight"}]
[{"name":"Clarissa Coomer"}]
的print_r($内容)
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 12 [6] => 43 [7] => 74 [8] => 23 [9] => 67 [10] => 32 )
的print_r($好友列表)
Array ( [1] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Alix Earnshaw ) [original:protected] => Array ( [name] => Alix Earnshaw ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [2] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Horacio Esgate ) [original:protected] => Array ( [name] => Horacio Esgate ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [3] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Patrizia Anthony ) [original:protected] => Array ( [name] => Patrizia Anthony ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [4] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Ann-marie McLewd ) [original:protected] => Array ( [name] => Ann-marie McLewd ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [5] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Boote Poulsum ) [original:protected] => Array ( [name] => Boote Poulsum ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [12] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Any Bandy ) [original:protected] => Array ( [name] => Any Bandy ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [43] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Francene Lelliott ) [original:protected] => Array ( [name] => Francene Lelliott ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [74] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Ingelbert Abdy ) [original:protected] => Array ( [name] => Ingelbert Abdy ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [23] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Nickolaus Di Biaggi ) [original:protected] => Array ( [name] => Nickolaus Di Biaggi ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [67] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Constantina Wreight ) [original:protected] => Array ( [name] => Constantina Wreight ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [32] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Clarissa Coomer ) [original:protected] => Array ( [name] => Clarissa Coomer ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) )
答案 0 :(得分:2)
使用first()代替get()
-
$buddylist[$value] = User::select('fullname as name')->where('id', $value)->first();
在您看来 -
@foreach($buddylist as $buddy)
<ul>
<li>{{ $buddy->name }}</li>
</ul>
@endforeach