Home > front end >  Getting an error trying to display all rows with the same name on Laravel
Getting an error trying to display all rows with the same name on Laravel

Time:04-14

Hi I am trying to create a view button on each row in my table which when clicked, allows the user to see all the rows with the same name as the specific row. However, when I click the view button I get this error:

Property [model_name] does not exist on this collection instance.

Here's my button code:

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

Here's my method code in the controller:

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

And here's my routing code:

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

Appreciate any help, thank you.

Edit:

Here's my 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 >
            All Models<b></b>
            
            </b>
        </h2>
        
    </x-slot>
    <button  position="fixed" onclick="location.href='{{ route('add.models') }}'" > </button>
    <div >
        <div >
            <div >
                <div >
                    @if(session('success'))
                        <div  role="alert">
                            <strong>{{ session('success') }}</strong>
                            <button type="button"  data-bs-dismiss="alert" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                            </button>
                          </div>
                          @endif                    
                    <div >

                        
                        <div >{{ $items->model_name }}</div>
                   


                <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 > 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) }}">View</a>
                            <a href="{{ url('model/edit/'.$item->id) }}">Edit</a>
                            <a href="{{ url('model/delete/'.$item->id) }}">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>

CodePudding user response:

This is the line that needs a change:

<div >{{ $items->model_name }}</div>

You are trying to get a property (model_name) from a collection (items). But the collection contains records that have model_name.

To show the model_name, you can pass it from controller to the view:

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

and then change

<div >{{ $items->model_name }}</div>

to

<div >{{ $model_name }}</div>

And for firstitem error, as the error indicates, there is no such method, change this line:

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

to something else, for example:

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