[英]I have data in array and have to show in Laravel blade template but its showing error when try to get name
我必須在不使用循環的情況下獲取所有組織名稱,因為我不能使用兩個循環。 我在 Blade 中想要做什么:
<?php $number = 0; $org_names_counter = 0;?>
@foreach($managers as $items)
{{-- @foreach($org_names as $key => $org_name) --}}
<?php $number++; ?>
<tr>
<td>{{$number}}</td>
<td>{{$items->name}}</td>
<td>{{$items->email}}</td>
<td> <?php echo $org_names[$org_names_counter]['name']; ?> </td>
<td>
<a href='editmanagers/{{ $items->id }}' class="edit" title="Edit" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
<a href='deletemanagers/{{ $items->id }}' class="delete" title="Delete" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
</td>
</tr>
@endforeach
這是我的 controller 代碼:
public function managersdata()
{
if (Auth::user()->role == "admin") {
$org_names = [];
$data = Managers::where('role', 'managers')->get();
foreach ($data as $datas) {
$org_names[] = Organization::where('id', $datas->organization_id)->get();
}
$managers = Managers::where('role', 'managers')->get();
return view('users.managersdetails', compact('managers', 'org_names'));
}
}
我收到此錯誤:
未定義索引:名稱(查看:/Applications/XAMPP/xamppfiles/htdocs/EASI/resources/views/users/managersdetails.blade.php)
這是我得到的 output:
Array ( [0] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Organization Object ( [table:protected] => organization [primaryKey] => id [timestamps] => [fillable:protected] => Array ( [0] => name [1] => email [2] => city ) [connection:protected] => mysql [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 2 [name] => test 4 [email] => test@gmail.com [city] => jhgkjhk ) [original:protected] => Array ( [id] => 2 [name] => test 4 [email] => test@gmail.com [city] => jhgkjhk ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [1] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Organization Object ( [table:protected] => organization [primaryKey] => id [timestamps] => [fillable:protected] => Array ( [0] => name [1] => email [2] => city ) [connection:protected] => mysql [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 5 [name] => test1 [email] => test1@gmail.com [city] => test ) [original:protected] => Array ( [id] => 5 [name] => test1 [email] => test1@gmail.com [city] => test ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [2] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Organization Object ( [table:protected] => organization [primaryKey] => id [timestamps] => [fillable:protected] => Array ( [0] => name [1] => email [2] => city ) [connection:protected] => mysql [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 3 [name] => test 2 [email] => test@gmail.com [city] => hhhhhhhhhhh ) [original:protected] => Array ( [id] => 3 [name] => test 2 [email] => test@gmail.com [city] => hhhhhhhhhhh ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [3] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Organization Object ( [table:protected] => organization [primaryKey] => id [timestamps] => [fillable:protected] => Array ( [0] => name [1] => email [2] => city ) [connection:protected] => mysql [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 2 [name] => test 4 [email] => test@gmail.com [city] => jhgkjhk ) [original:protected] => Array ( [id] => 2 [name] => test 4 [email] => test@gmail.com [city] => jhgkjhk ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [4] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Organization Object ( [table:protected] => organization [primaryKey] => id [timestamps] => [fillable:protected] => Array ( [0] => name [1] => email [2] => city ) [connection:protected] => mysql [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 2 [name] => test 4 [email] => test@gmail.com [city] => jhgkjhk ) [original:protected] => Array ( [id] => 2 [name] => test 4 [email] => test@gmail.com [city] => jhgkjhk ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) )
您看到的錯誤:
未定義的索引名稱
告訴我們這是問題所在:
echo $org_names[$org_names_counter]['name'];
查看 controller 代碼,該變量設置為:
$org_names[] = Organization::where('id', $datas->organization_id)->get();
Eloquent 方法get()
將返回一個集合,因此$org_names
是一個 collections 數組。 $org_names[0]
是Organization
的集合,而不是單個Organization
。
您還可以在顯示的轉儲中看到這一點:
Array (
[0] => Illuminate\\Database\\Eloquent\\Collection Object (
$org_names[0]
是一個集合。
要訪問這些,您必須執行以下操作:
foreach ($org_names[$org_names_counter] as $org) {
echo $org->name;
// ...
}
但是查看代碼,很明顯集合中應該只有 1 個元素,因為您是按 ID 選擇的。 在這種情況下,您追求的是單一結果。 要獲得單個匹配結果,請使用:
$org_names[] = Organization::where('id', $datas->organization_id)->first();
現在$org_names[]
只是一個Organization
數組,您現有的視圖代碼將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.