繁体   English   中英

我在数组中有数据,必须在 Laravel 刀片模板中显示,但在尝试获取名称时显示错误

[英]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">&#xE254;</i>
            </a>
            <a href='deletemanagers/{{ $items->id }}' class="delete" title="Delete" data-toggle="tooltip">
                <i class="material-icons">&#xE872;</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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM