This returns a Builder instance; not a Model instance:
$listItem->PageSeen()
You want the result of the Relationship
$listItem->PageSeen->menu
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
PageVisitor.php
class PageVisitor extends Model
{
use HasFactory;
public function PageSeen()
{
return $this->belongsTo(PageSeen::class, 'id', 'menu_id');
// return $this->belongsTo(PageSeen::class, 'menu_id', 'id');
}
}
PageSeen.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use App\Model\PageVisitor;
class PageSeen extends Model
{
//
protected $table = 'page_seen';
public function PageVisitor()
{
// return $this->hasMany(PageVisitor::class, 'id', 'menu_id');
return $this->hasMany(PageVisitor::class, 'menu_id', 'id');
}
}
Index.php
@php
$hasil = array();
@endphp
@foreach ($list as $listItem)
@php
$hasil[] = ["label" => $listItem->PageSeen()->menu, "value" => count((array)$listItem->date)];
@endphp
@endforeach
For some reason, I forget how to do the table relation since it's been a while and I think I am going to redocument it so that I won't forget:
ref: https://laravel.com/docs/8.x/eloquent-relationships
page_visitors
----------------
id
menu_id
ip
date
page_seen
----------------
id
menu
view
Please or to participate in this conversation.