php - Why do I get a "Trying to get property of non-object" error when using Sortable package in Laravel?
one text
I've encountered a problem with the Kyslik\Column-sortable package for Laravel which has frustrated me for weeks now. :) The sorting links displays correctly, but when I click on one of the sorting links, I get the following error: ??�? Trying to get property 'title' of non-object (View: C:\wamp64\www\GeekCollector\resources\views\Lp\show.blade.php) What's even more strange is that the route goes to the BrowseResults view, but the message sais that the error was encountered in the Show view. If I remove all the sortable related stuff from the BrowseResults view, the Show view works perfectly. Here is my model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;
class Lp extends Model
{
use HasFactory;
use Sortable;
public $sortable = ['title', 'artist', 'year'];
}
Here is my BrowseResults controller method:
public function browseResults(Request $request)
{
$chosenCategory = $request->category;
$lps = Lp::where('category','like','%'.$chosenCategory.'%');
$result = $lps->sortable('title')->paginate(5);
return view('Lp.browseResults', compact('result'));
}
And here is my blade view:
<x-app-layout>
<x-slot name="header">
<h1 class="font-semibold text-xl text-gray-800 leading-tight">
Lp-skivor
</h1>
</x-slot>
@sortablelink('title')
@sortablelink('artist')
@sortablelink('year')
<table>
<tr>
<td>Titel</td>
<td>Artist</td>
<td>Kategori</td>
<td>Plats</td>
<td>Position</td>
</tr>
@foreach ($result as $lp)
<tr>
<td>
<a href="{{ route('lp.show', $lp->id) }}">{{ $lp->title }}</a>
</td>
<td>{{ $lp->artist }}</td>
<td> {{ $lp->category }}</td>
<td>{{ $lp->place }}</td>
<td>{{ $lp->position }}</td>
<td>
<a href="{{ route('lp.edit', $lp->id) }}">Uppdatera</a>
</td>
<td>
<form action="{{ route('lp.destroy', $lp->id) }}" method="post">
@csrf
@method('DELETE')
<button type="submit">Ta bort</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $result->appends(\Request::except('page'))->render() !!}
</x-app-layout>
The route looks like this:
Route::post('/lp/browse-results', [LpController::class, 'browseResults'])->name('lp.browseResults');
Thanks very much in advance for your help!
All the best!
Source