Many to many relationship in Laravel

Nitish Kumar

I'm trying to build an application on Laravel 5.3 where my models, database and controllers are in separate folders. I have the following folder structure:

Nitseditor
    System
        Controllers
        Database
            2016_12_28_130149_create_domains_table.php
            2017_01_06_193355_create_themes_table.php
            2017_01_07_140804_create_themes_domains_table.php
        Models
            Domain.php
            Theme.php

I'm making a relationship in domain with many to many relationship i.e.

public function themes()
{
    return $this->belongsToMany('Nitseditor\System\Models\Domain');
}

I've named the table domain_theme inside the 2017_01_07_140804_create_themes_domains_table.php

Now I'm trying to get the theme name which belongs to the domain in the controller something like this:

$flashmesage = new Domain;

foreach ($flashmesage->themes as $theme)
{
    return $theme->theme_name;
}

I'm getting an error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'nitswebbuilder.domain_domain' doesn't exist (SQL: select domains.*, domain_domain.domain_id as pivot_domain_id from domains inner join domain_domain on domains.id = domain_domain.domain_id where domain_domain.domain_id is null and domains.deleted_at is null)

tuytoosh

sorry for my short comment as answer... I have not enough reputation for comment,

change your themes() method to :

public function themes()
{
    return $this->belongsToMany('Nitseditor\System\Models\Theme');
}

see Here for more info

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel - Many to Many relationship

From Dev

Many to Many relationship with Laravel

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: one to many relationship

From Dev

one to many relationship in laravel

From Dev

Laravel Polymorphic many to many relationship issue

From Dev

Laravel 5.2 Friendship system: Many to Many relationship

From Dev

Laravel many-to-many relationship on the same model

From Dev

How to get many to many relationship items in laravel

From Java

Laravel save / update many to many relationship

From Dev

Is this many-to-many relationship possible in Laravel?

From Dev

Eager loading with many to many relationship in laravel 4

From Dev

Laravel Complicated inner join on many to many relationship

From Dev

Friendship system with Laravel : Many to Many relationship

From Dev

laravel syncing many-to-many relationship

From Dev

Laravel many to many relationship using Eloquent

From Dev

how to define many to many relationship in laravel models?

From Dev

Advanced search with many to many relationship in laravel

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel Many to many relationship get specific data

From Dev

Laravel Many to Many Relationship - Linking 3 items

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Is this many-to-many relationship possible in Laravel?

From Dev

Many to Many relationship query. Laravel

Related Related

  1. 1

    Laravel - Many to Many relationship

  2. 2

    Many to Many relationship with Laravel

  3. 3

    Laravel 6 many to many relationship

  4. 4

    Pagination with many to many relationship in laravel

  5. 5

    Laravel many to many relationship error

  6. 6

    Laravel many to many relationship with conditions

  7. 7

    Laravel ORM many to many relationship

  8. 8

    Laravel: one to many relationship

  9. 9

    one to many relationship in laravel

  10. 10

    Laravel Polymorphic many to many relationship issue

  11. 11

    Laravel 5.2 Friendship system: Many to Many relationship

  12. 12

    Laravel many-to-many relationship on the same model

  13. 13

    How to get many to many relationship items in laravel

  14. 14

    Laravel save / update many to many relationship

  15. 15

    Is this many-to-many relationship possible in Laravel?

  16. 16

    Eager loading with many to many relationship in laravel 4

  17. 17

    Laravel Complicated inner join on many to many relationship

  18. 18

    Friendship system with Laravel : Many to Many relationship

  19. 19

    laravel syncing many-to-many relationship

  20. 20

    Laravel many to many relationship using Eloquent

  21. 21

    how to define many to many relationship in laravel models?

  22. 22

    Advanced search with many to many relationship in laravel

  23. 23

    Laravel Many to Many query relationship with where clause

  24. 24

    Laravel 5 Eloquent count many to many relationship

  25. 25

    Laravel Many to many relationship get specific data

  26. 26

    Laravel Many to Many Relationship - Linking 3 items

  27. 27

    Laravel Removing Pivot data in many to many relationship

  28. 28

    Is this many-to-many relationship possible in Laravel?

  29. 29

    Many to Many relationship query. Laravel

HotTag

Archive