php - 正确显示关联数组的结果 Laravel/PHP

标签 php mysql arrays laravel

我试图在 Laravel 中显示一个查询中的姓名列表,但我无法正确显示结果。

这是我的 Controller 中的内容:

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=>$key 它什么都不显示。 如果我改用它,结果如下所示:

[{"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] => * ) ) ) ) )

最佳答案

使用first()而不是 get() -

$buddylist[$value] = User::select('fullname as name')->where('id', $value)->first();

在你看来——

@foreach($buddylist as $buddy)
<ul>
    <li>{{ $buddy->name }}</li>
</ul>
@endforeach

关于php - 正确显示关联数组的结果 Laravel/PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48483327/

相关文章:

php - 如何使用 while 循环将 4 个项目排成一行?

javascript - 在dojo中找到ajax请求

PHP - 数组映射或过滤

php - 从mysql数据库获取1列值

php - 修改数组中指定键的每一个值

javascript - .join 不是函数

c++ - 关联数组、数组和树

php - 将对象作为 json 返回时访问 Eloquent 关系

php - 使用 javascript 获取 php/mysql 值

php - 带有MySQL查询结果的数组