Laravel 5 Many to Many - Table name in singular

Luciano Nascimento

MySQL Tables:

- category
- unit
- category_unit (many to many)
    - category_id
    - unit_id

Laravel 5 Models:

<?php

class Unit extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'unit';
}

class Category extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'category';

    public function units()
    {
        return $this->morphToMany('App\Unit', 'category_unit'); // Table not in plural.
    }
}

Controller Code:

$category = Category::find($id);
var_dump($category->units);

Error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.category_units' doesn't exist (SQL: select `unit`.*, `category_units`.`category_unit_id` as `pivot_category_unit_id`, `category_units`.`unit_id` as `pivot_unit_id` from `unit` inner join `category_units` on `unit`.`id` = `category_units`.`unit_id` where `category_units`.`category_unit_id` = 1 and `category_units`.`category_unit_type` = App\Category)

Laravel 5 is trying to find the table category_unit as the plural category_units. As my database is not new and I already used it in production servers, I cannot change the table name.

How can I do to Laravel 5 use it with singular name?

Mina Abadir

The problem here is that you are trying to create Many to Many relationship using a polymorphic one.

The morphToMany() method doesn't take the table name as the second argument. I think your case is simpler, just change the relation to belongsToMany()

So your code should be

class Category extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'category';

    public function units()
    {
        return $this->belongsToMany('App\Unit', 'category_unit'); // Table not in plural.
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change name of generated Join table ( Many to Many ) - EF Core 5

From Dev

Many to many relationship in laravel need a specific name for the table?

From Dev

Laravel 5 Eloquent Many to Many 2ndary table

From Dev

laravel many to many with third table

From Dev

Laravel 5 Eloquent ORM - Many to Many through Many to Many

From Dev

What decides the table name in many to many relations

From Dev

Laravel Eloquent many to many filter on intermediate table

From Dev

Laravel: many-to-many with shared table

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Laravel Many-to-Many Pivot Table

From Dev

save many to many relation in Laravel elequent 5?

From Dev

Laravel 5 Validate many to many data

From Dev

Laravel 5 - Many to Many - Attach versus Save

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel 5 Validate many to many data

From Dev

save many to many relation in Laravel elequent 5?

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

Laravel eloquent - Many to Many, select only matching the many table

From Dev

Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

From Dev

Laravel - many-to-many where the many-to-many table is (part-) polymorph

From Dev

has_and_belongs_to_many Association table name

From Dev

Laravel - Many to Many relationship

From Dev

Laravel - Many To Many

From Dev

Laravel Many to Many relation

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Many to Many laravel

From Dev

Many to many MVC 5 Model Code first join table

From Dev

How express the many to many relationship with single parent table in EF 5

Related Related

  1. 1

    Change name of generated Join table ( Many to Many ) - EF Core 5

  2. 2

    Many to many relationship in laravel need a specific name for the table?

  3. 3

    Laravel 5 Eloquent Many to Many 2ndary table

  4. 4

    laravel many to many with third table

  5. 5

    Laravel 5 Eloquent ORM - Many to Many through Many to Many

  6. 6

    What decides the table name in many to many relations

  7. 7

    Laravel Eloquent many to many filter on intermediate table

  8. 8

    Laravel: many-to-many with shared table

  9. 9

    Laravel 4 - Many to Many - pivot table not updating

  10. 10

    Laravel Many-to-Many Pivot Table

  11. 11

    save many to many relation in Laravel elequent 5?

  12. 12

    Laravel 5 Validate many to many data

  13. 13

    Laravel 5 - Many to Many - Attach versus Save

  14. 14

    Laravel 5 Eloquent count many to many relationship

  15. 15

    Laravel 5 Validate many to many data

  16. 16

    save many to many relation in Laravel elequent 5?

  17. 17

    Laravel 5 | Many to Many Relationship not working

  18. 18

    Laravel eloquent - Many to Many, select only matching the many table

  19. 19

    Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

  20. 20

    Laravel - many-to-many where the many-to-many table is (part-) polymorph

  21. 21

    has_and_belongs_to_many Association table name

  22. 22

    Laravel - Many to Many relationship

  23. 23

    Laravel - Many To Many

  24. 24

    Laravel Many to Many relation

  25. 25

    Many to many relationship in Laravel

  26. 26

    Many to Many relationship with Laravel

  27. 27

    Many to Many laravel

  28. 28

    Many to many MVC 5 Model Code first join table

  29. 29

    How express the many to many relationship with single parent table in EF 5

HotTag

Archive