Hi All - I have an event. Each event has Participants and each Participant eager loads with a User object. I have columns for first,last, and middle names in the User table.
I've set my Livewire component to paginate the Participants, and this is working as expected but ordering the Participants by their participant.id.
I want the participants ordered by last/first and, so far, have NOT found the right way to make this work. I'm hoping you can give me some guidance!
Here's the livewire ParticipantsComponent:
<?php
namespace App\Http\Livewire\Events\Versions;
use App\Models\Events\Participants\Participant;
use App\Models\Events\Participants\ParticipantStatus;
use Livewire\Component;
use Livewire\WithPagination;
class ParticipantsComponent extends Component
{
use WithPagination;
public $dto;
public $formToggle=false;
public $participantStatuses;
public $participantStatusId=0;
public $toggleParticipants=true;//false;
public $userId;
public $name='';
public function render()
{
return view('livewire..events.versions.participants-component',
[
'participants' => Participant::paginate(),
]);
}
}
and the Participant model:
<?php
namespace App\Models\Events\Participants;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Participant extends Model
{
use HasFactory;
protected $fillable = ['participant_status_id', 'user_id', 'version_id'];
protected $with = ['user'];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
and the User model. The fullNameAlpha is delivered through the nameParts($value) method:
<?php
namespace App\Models;
use App\Models\Teachers\School;
use App\Models\Teachers\Student;
use App\Models\Teachers\Teacher;
use App\Models\Utilities\Name;
use App\Notifications\MailResetPasswordToken;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'email',
'first',
'last',
'middle',
'name',
'password',
];
}
I've tried creating a custom LengthAwarePaginator:
private function participantsSorted(): LengthAwarePaginator
{
$participants = Participant::where('version_id', 72)
->take(10)
->get()
->sortBy(['user.last','user.first']);
$lap = new LengthAwarePaginator(
$participants->forPage(1,15),
$participants->count(),
15,
1,
);
return $lap;
}
and then changing the render() method to:
public function render()
{
return view('livewire..events.versions.participants-component',
[
'participants' => $this->participantsSorted()->paginate(),
]);
}
but this resulted in the error: Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
All help is appreciated!