Skip to content

Commit

Permalink
User model add last event method
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 24, 2024
1 parent 95d5701 commit 6003a65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -68,6 +69,11 @@ public function activity(): HasMany
return $this->hasMany(UserActivity::class);
}

public function lastSeen(): Model | null
{
return $this->activity()->latest()->first();
}

public function getAvatarAttribute(): string
{
return 'https://api.dicebear.com/8.x/identicon/svg?seed=' . $this->name;
Expand Down
16 changes: 13 additions & 3 deletions resources/views/livewire/user-show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@
<p class="text-gray-500 text-xs">Joined {{ $user->created_at->diffForHumans() }}</p>
</div>
<div>
<p class="text-gray-500 text-xs">Last seen ...</p>
<p class="text-gray-500 text-xs">Last seen
@if($user->lastSeen())
{{ $user->lastSeen()->created_at->diffForHumans() }}
@else
Never
@endif
</p>
</div>
</div>

Expand All @@ -69,7 +75,7 @@
$recent_orders = $user->orders->take(3);
@endphp

@foreach ($recent_orders as $order)
@forelse ($recent_orders as $order)
<div class="bg-gray-50 p-4 rounded-lg mb-4">
<div class="flex items-center justify-between">
<div>
Expand All @@ -85,7 +91,11 @@ class="text-blue-500">
</div>
<p class="mt-2 text-gray-700">{{ $order->description }}</p>
</div>
@endforeach
@empty
<div class="bg-gray-50 p-4 rounded-lg mb-4">
<p class="text-gray-700">No recent orders</p>
</div>
@endforelse
</div>
</div>
</div>
Expand Down

0 comments on commit 6003a65

Please sign in to comment.