簡體   English   中英

此集合實例上不存在 laravel 屬性 [id]

[英]laravel Property [id] does not exist on this collection instance

我無法訪問我的 admin_table 我知道我在 admin_table 上的路線上有問題,請幫助我解決我的路線屬性 [id] 在此集合實例中不存在。

我的 admin_table.blade

@foreach ($users as $positions)
    @foreach ($positions->admins as $position )
        <tbody>
        <tr>
            <td>{{ $position->id}}</td>
            <td>{{ $position->first_name}}</td>
            <td>{{ $position->last_name}}</td>
            <td>{{ $position->contact}}</td>
            <td>{{ $position->departments->department}}</td>
            <td>{{ $position->schoolpositions->school_position}}</td>
            <td>{{ $position->email}}</td>
            <th> <a href="{{action('DashboardController@edit',$position['id' =>$position->id])}} " class="btn btn-success">Edit </a>
            </th>
        </tr>
        </tbody>
    @endforeach

    </tr>
    </tbody>
@endforeach
@endforeach
</table>

這是我的路線

Route::get('/admin_table', 'DashboardController@index');
Route::put('/admin_table/{id}', 'DashboardController@store');
Route::get('/admin_table', 'DashboardController@show');
Route::get('/teacher_editform/{id}', 'DashboardController@edit');
Route::put('/teacher_editform/{id}', 'DashboardController@update')->name('teacheradminpage.teacher_tableform.teacher_editform');

這是我的控制器

public function edit($id)
{
    $departments =  Department::find($id);
    $users =  Schoolposition::find($id);
    $students =  Student::find($id);
    $admins =  Admin::find($id);
    return view('teacheradminpage.teacher_tableform.teacher_editform', compact('departments','users','students','admins', 'id', 'id', 'id', 'id'));
}

您的問題是您在數組之前放置了一個額外的$position 你的路線應該是

<a href="{{action('DashboardController@edit',['id' =>$position->id])}} " class="btn btn-success">Edit </a>

您在此代碼中有幾個可能需要注意的問題。 position錯誤的主要問題似乎是您有一個圓形路徑可以在循環內的anchor選項卡中獲取$position

本節可能不會創建您想要的內容:

@foreach ($users as $positions)

     @foreach ($positions->admins as $position )

這大概是說 - 使用我的用戶集合並創建一個新的位置集合,這些位置直接附加到每個user對象。 從這里,乘坐的該集合positions和試圖找到管理員的集合上附加到該集合,然后找到一個$position (我認為這與它試圖做的很接近......)

當您到達循環內部和您的anchor / route ,您會要求anchor在可能不存在或再次收集的東西上找到一個id

您已經將admins傳遞到此視圖。 將其用作頂級循環項目是有意義的,不是嗎?

要修復,請使用admins作為唯一的循環項:

 @foreach ($admins as $position )

編輯你的刀片

@foreach ($users as $positions)
@if(!empty($positions->admins)
@foreach ($positions->admins as $position )
    <tbody>
    <tr>
        <td>{{ $position->id}}</td>
        <td>{{ $position->first_name}}</td>
        <td>{{ $position->last_name}}</td>
        <td>{{ $position->contact}}</td>
        <td>{{ $position->departments->department}}</td>
        <td>{{ $position->schoolpositions->school_position}}</td>
        <td>{{ $position->email}}</td>
        <th> <a href="{{action('DashboardController@edit',$position['id' =>$position->id])}} " class="btn btn-success">Edit </a>
        </th>
    </tr>
</tbody>
@endforeach
</tr>
</tbody>
@endforeach
@endif
@endforeach                                
</table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM