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

arctushar's avatar

Many to many showing only few rows

I have Two models with three table. Models are Node & Tag. And have Three tables named- nodes, tags, node_tag. My Node Model code is as below

    public function tags(){
        return $this->belongsToMany('App\Tag');
    }

My Tag model code is as below

    public function nodes(){
        return $this->belongsToMany('App\Node');
    }

When I call in controller as below

return $node=Node::find(1723)->tags;

this is showing only two rows. But when I call in controller as below

return $user = DB::table('node_tag')->where('node_id', 1723)->get();

It shows four rows. What is the problem ?

0 likes
10 replies
arctushar's avatar

@bobbybouwmann I tried, same result, only two tags rows

$node = Node::select('id')->with('tags')->find(1723);
return $node;

Giving Result

{"id":1723,"tags":[{"id":58,"name":"civil","created_at":"2018-05-23 14:15:51","updated_at":"-0001-11-30 00:00:00","pivot":{"node_id":1723,"tag_id":58}},{"id":168,"name":"general","created_at":"2018-05-23 14:15:51","updated_at":"-0001-11-30 00:00:00","pivot":{"node_id":1723,"tag_id":168}}]}

Yorki's avatar

What result does second implementation return?

return $user = DB::table('node_tag')->where('node_id', 1723)->get();
_Artak_'s avatar

@arctushar

Maybe the model has Global scope, e.g. soft delete and in the "tags" table they are removed. there's no scope in the DB::table

if you have soft delete try something like this


        $node = Node::with('tags' => function($query){
                $query->withTrashed();
        })->find(1723);

return $node;

arctushar's avatar

@Artak For your cod error is

syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')'

Your last code need [ ] for with , I changed it to with

    $node = Node::with(['tags' => function($query){
                $query->withTrashed();
        }])->find(1723);

In this case error is

Method Illuminate\Database\Query\Builder::withTrashed does not exist.
arctushar's avatar

Node model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Node extends Model
{
    public function users(){
        return $this->belongsToMany('App\User');
    }
    public function tags(){
        return $this->belongsToMany('App\Tag');
    }
    public function statistics(){
        return $this->belongsToMany('App\Statistic');
    }   
    public function nodedescription(){
        return $this->hasOne('App\Nodedescription');
    }   
}

Tag model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    public function nodes(){
        return $this->belongsToMany('App\Node');
    }
}
bobbybouwmann's avatar

Can you show the data in the database? Are you sure not two of them are deleted?

I've never seen a case where Laravel is not returning all results when you retrieve data!

Majeed's avatar

You can try this way.

 public function tags(){
        return $this->belongsToMany('App\Tag');
    }
 public function nodes(){
        return $this->belongsToMany('App\Node');
    }

fetch data from pivot or intermediate table.

$node=Node::with('tags')->get();
return $node;

**It will show the Json Data. **

staudenmeir's avatar

What's the result of dd(Node::find(1723)->tags()->toSql());?

Please or to participate in this conversation.