尝试在 Laravel 上显示具有相同名称的所有行时出错

Getting an error trying to display all rows with the same name on Laravel

您好,我正在尝试在我的 table 中的每一行上创建一个查看按钮,单击该按钮后,用户可以查看与特定行同名的所有行。但是,当我单击查看按钮时出现此错误:

属性 [model_name] 在此集合实例上不存在。

这是我的按钮代码:

<a href="{{ url('model/view/'.$item->model_name) }}"class="btn btn-success">View</a>

这是我在控制器中的方法代码:

public function View($model_name){
        $items = Item::where('model_name',$model_name)->get();
        
        return view('admin.item.view',compact('items'));
    }

这是我的路由代码:

Route::get('/model/view/{model_name}', [ItemController::class,'View'])->name('view.models');

感谢任何帮助,谢谢。

编辑:

这是我的 view.blade.php:

<style>
    .kc_fab_main_btn {
        position:fixed;
        right:20px;
        bottom:20px;
        background-color: #F44336;
        width: 60px;
        height: 60px;
        border-radius: 100%;
        background: #8fce00;
        border: none;
        outline: none;
        color: #FFF;
        font-size: 36px;
        box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
        transition: .3s;
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
      }
      
      .kc_fab_main_btn:focus {
        transform: scale(1.1);
        transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -webkit-transform: rotate(45deg);
      }
    </style>
<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            All Models<b></b>
            
            </b>
        </h2>
        
    </x-slot>
    <button class="kc_fab_main_btn" position="fixed" onclick="location.href='{{ route('add.models') }}'" >+</button>
    <div class="py-12">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    @if(session('success'))
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <strong>{{ session('success') }}</strong>
                            <button type="button" class="close" data-bs-dismiss="alert" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                            </button>
                          </div>
                          @endif                    
                    <div class="card">

                        
                        <div class="card-header">{{ $items->model_name }}</div>
                   


                <table class="table">
                    <thead>
                      <tr>
                        <th scope="col">SL No</th>
                        <th scope="col">Model Name</th>
                        <th scope="col">Model Image</th>
                        <th scope="col">Description</th>
                        <th scope="col">Version</th>
                        <th scope="col">Created At</th>
                        <th scope="col">Modify</th></th>
                      </tr>
                    </thead>
                    <tbody>
                        
                        @foreach($items as $item)
                        
                      <tr>
                        <th scope="row">{{ $items->firstItem()+$loop->index}}</th>
                        <td>{{ $item->model_name }}</td>
                        <td><model-viewer src="{{ asset($item->model_image) }}"alt=""auto-rotate="" camera-controls="" background-color="#455A64"></model-viewer></td> {{-- Eloquent ORM --}}
                        
                        <td>{{ $item->description }}</td>
                        <td>{{ $item->version }}</td>
                        <td>
                            @if($item->created_at == NULL)
                            
                            <span class="text-danger"> No Date Set </span>
                            @else
                            {{ Carbon\Carbon::parse($item->created_at)->diffForHumans() }} {{-- Carbon needed for query builder --}}
                            @endif
                            
                        
                        
                        </td> 
                        <td>
                            <a href="{{ url('model/view/'.$item->name) }}"class="btn btn-info">View</a>
                            <a href="{{ url('model/edit/'.$item->id) }}"class="btn btn-info">Edit</a>
                            <a href="{{ url('model/delete/'.$item->id) }}"class="btn btn-danger">Delete</a>
                        
                        </td>
                      </tr>

                      @endforeach
                      
                     
                    </tbody>
                  </table>
                  {{ $items->links() }}
                </div>
            </div>
            
        </div>
    </div>
</div>


</div>
</div>
</div>





    </div>
    <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.js"></script>
    <!-- Loads model-viewer for older browsers -->
    <script nomodule src="https://unpkg.com/@google/model-viewer/dist/model-viewer-legacy.js"></script>
    
</x-app-layout>

这是需要更改的行:

<div class="card-header">{{ $items->model_name }}</div>

您正在尝试从 collection(项目)中获取 属性(model_name)。但是该集合包含 model_name.

的记录

要显示 model_name,您可以将其从控制器传递到视图:

public function View($model_name){
        $items = Item::where('model_name',$model_name)->get();
        
        return view('admin.item.view',compact('items', 'model_name'));
    }

再改

<div class="card-header">{{ $items->model_name }}</div>

<div class="card-header">{{ $model_name }}</div>

而对于firstitem错误,如错误提示,没有这样的方法,更改此行:

<th scope="row">{{ $items->firstItem()+$loop->index}}</th>

到别的东西,例如:

{{ $items->perPage() * ($items->currentPage() - 1) + $loop->iteration }}