Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Illuminate\Database\Eloquent\Relations\BelongsTo::$menu

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
0 likes
9 replies
tykus's avatar

This returns a Builder instance; not a Model instance:

$listItem->PageSeen()

You want the result of the Relationship

$listItem->PageSeen->menu
Sinnbeck's avatar

@davy_yg A Builder is a query builder.. Like ->where('x', 1) and similar.. It builds the query for the database..

davy_yg's avatar
Level 27

I also get another error: Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'page_seen.menu_id' in 'where clause' (SQL: select * from page_seen where page_seen.menu_id = 1 limit 1) (View: E:\xampp76\htdocs\zws_admin_8\resources\views\admin\index.blade.php)

Why is it looking for 'page_seen.menu_id' ?

The connection should from page_seen.id ---> page_visitors.menu_id

Sinnbeck's avatar

@davy_yg Because you told it to

public function PageSeen()
    {
        return $this->belongsTo(PageSeen::class, 'id', 'menu_id');   //menu_id 
    }
davy_yg's avatar
Level 27

I thought the menu_id will belongs to PageSeen connection to PageVisitor ?

If I delete the menu_id then:

ErrorException Trying to get property 'menu' of non-object (View: E:\xampp76\htdocs\zws_admin_8\resources\views\admin\index.blade.php)

davy_yg's avatar
Level 27

From the doc:

	/**
	* Get the user that owns the phone.
	*/

	public function user()
	{
			return $this->belongsTo(User::class, 'foreign_key', 'owner_key');
	}

Then I assume I should write it this way:

	class PageVisitor extends Model
	{
	use HasFactory;

	public function PageSeen()
	{
    	// return $this->belongsTo(PageSeen::class, 'id');   
    	return $this->belongsTo(PageSeen::class, 'menu_id', 'id'); 
	}
    
}

From this doc:

		return $this->hasMany(Comment::class, 'foreign_key', 'local_key');

Then I assume:

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');
	}

	}

Okay, from this doc:

		return $this->hasMany(Comment::class, 'foreign_key');

		return $this->hasMany(Comment::class, 'foreign_key', 'local_key');

Then, I assume:

		class PageVisitor extends Model
		{
		use HasFactory;

	    public function PageSeen()
		{
    		// return $this->belongsTo(PageSeen::class, 'id');   
    		return $this->belongsTo(PageSeen::class, 'menu_id', 'id'); 
		}     
	}

or perhaps because I haven't set foreign key in my table ?

ref: https://www.youtube.com/watch?v=2YD8ktyEGbs

Please or to participate in this conversation.