Empty data returning from many to many relationship laravel 5

moin khan

I've two model called application and user.

Application

id | name

user

id | first name

application_user

id | application_id | user_id | comment| rating | like

Here is the model classes

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\ModelValidator;

class Application extends ModelValidator
{
    //
    protected $table = "application";

    public function user()
    {
        return $this->belongsToMany('App\User','application_user','application_id','user_id')->withPivot(['rating','like','comment'])->withTimestamps();
    }

    public function getComment()
    {
        $comments = array();

        foreach ($this->user as $user) {
            $comments[] = $user->comment;
        }

        return compact($comments);
    }
}

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;

class User extends ModelValidator implements AuthenticatableContract,CanResetPasswordContract
{
    use Authenticatable,CanResetPassword;

    protected $table = 'user';

    public function application()
    {
        return $this->belongsToMany('App\Application')->withPivot(['rating','like','comment'])->withTimestamps();
    }
}

But when i run the below code it prints empty array.

$apps = \App\Application::all();

        foreach ($apps as $app) {
            print_r($app->getComment());
        }

Why its returning empty array ?

Thanks in advance

Renish Khunt

just change your relationship like

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\ModelValidator;

class Application extends ModelValidator
{
    //
    protected $table = "application";

    public function user()
    {
        return $this->hasMany('App\ApplicationUser','application_id','id')
               ->withPivot(['rating','like','comment'])->withTimestamps();
    }

}

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;

class User extends ModelValidator implements AuthenticatableContract,CanResetPasswordContract
{
    use Authenticatable,CanResetPassword;

    protected $table = 'user';

    public function application()
    {
        return $this->belongsToMany('App\Application')->withPivot(['rating','like','comment'])->withTimestamps();
    }
}

change your code like this.

foreach ($apps as $app) {
    print_r($app->user);
}

Now you get data from application_user table, you got empty result because of you trying to find a data into user table so for that you got empty result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Laravel/SQL returning data from many-to-many relationship as array of data

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

Laravel Many to many relationship get specific data

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Laravel 5 One-to-many relationship master and parent data CRUD

From Dev

Laravel 5.2 Many to Many Relationship inverse query empty results

From Dev

SQL for returning values from a many-to-many relationship

From Dev

Laravel 5 Validate many to many data

From Dev

Laravel 5 Validate many to many data

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel 5 Deleting a one-to-many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Building a dynamic query of a many-to-many relationship in Laravel 5

From Dev

Laravel 5 - access specific model on many to many relationship

From Dev

Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

From Dev

Laravel 6 many to many relationship

From Dev

Pagination with many to many relationship in laravel

From Dev

Laravel many to many relationship error

From Dev

Laravel many to many relationship with conditions

From Dev

Laravel ORM many to many relationship

From Dev

Laravel Get data One to Many Relationship with conditions

From Dev

Laravel get data from many to many relation

From Dev

Creating a Many-to-many relationship in Laravel with additional data

From Dev

How to sum total price from a Many to Many relationship collection in Laravel

Related Related

  1. 1

    Laravel/SQL returning data from many-to-many relationship as array of data

  2. 2

    Laravel 5 Eloquent count many to many relationship

  3. 3

    Laravel 5 | Many to Many Relationship not working

  4. 4

    Laravel Many to many relationship get specific data

  5. 5

    Laravel Removing Pivot data in many to many relationship

  6. 6

    Laravel - Many to Many relationship

  7. 7

    Many to many relationship in Laravel

  8. 8

    Many to Many relationship with Laravel

  9. 9

    Laravel 5 One-to-many relationship master and parent data CRUD

  10. 10

    Laravel 5.2 Many to Many Relationship inverse query empty results

  11. 11

    SQL for returning values from a many-to-many relationship

  12. 12

    Laravel 5 Validate many to many data

  13. 13

    Laravel 5 Validate many to many data

  14. 14

    Laravel 5 Eloquent Relationship - Has Many Though

  15. 15

    Laravel 5 Deleting a one-to-many relationship

  16. 16

    Laravel 5 one to many eloquent relationship

  17. 17

    Laravel 5 Eloquent Relationship - Has Many Though

  18. 18

    Building a dynamic query of a many-to-many relationship in Laravel 5

  19. 19

    Laravel 5 - access specific model on many to many relationship

  20. 20

    Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

  21. 21

    Laravel 6 many to many relationship

  22. 22

    Pagination with many to many relationship in laravel

  23. 23

    Laravel many to many relationship error

  24. 24

    Laravel many to many relationship with conditions

  25. 25

    Laravel ORM many to many relationship

  26. 26

    Laravel Get data One to Many Relationship with conditions

  27. 27

    Laravel get data from many to many relation

  28. 28

    Creating a Many-to-many relationship in Laravel with additional data

  29. 29

    How to sum total price from a Many to Many relationship collection in Laravel

HotTag

Archive