Get all records from another table with pivot

user1695123

I have three tables:

comments

id

comment_links

id | comment_id | link_id

links

id

I want to get all Links associated with each Comment. As you can see, CommentLink should act as a pivot table, but I don't know how to set up the relationships to make this work. I think I need to use the hasManyThrough relationship, but I'm not 100% sure.

Here are my current class relationships (they may be wrong):

Comment.php:

class Comment extends Model
{
    public function commentLinks()
    {
        return $this->hasMany('App\CommentLink', 'comment_id', 'id');
    }

    public function links()
    {
        // How do I fetch all links here using the pivot table?
    }
}

CommentLink.php:

class CommentLink extends Model
{
    public function comment()
    {
        return $this->belongsTo('App\Comment');
    }
    
    public function link()
    {
        return $this->hasOne('App\Link', 'id', 'link_id');
    }
}

Link.php:

class Link extends Model
{
    public function commentLinks()
    {
        return $this->belongsToMany('App\CommentLink', 'link_id', 'id');
    }
}

What do I need to do here to make this work?

Michael Richardson

You have the right idea, just using the wrong class names.

class Link extends Model
{
    public function comments()
    {
        return $this->belongsToMany('App\Comment');
    }
}

class Comment extends Model
{
    public function links()
    {
        return $this->belongsToMany('App\Link');
    }
}

You don't actually need to have a class for your pivot table, but you can if you want.

This is quite a good guide on how to do this. https://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

get records without records from another table

From Dev

Get all records from Model that do NOT have an entry in pivot table Laravel 5

From Dev

How to get 2 id from tow column and get all records from another table

From Dev

Get all data from pivot table

From Dev

How to get all records from one table and save it on another table by c# in winforms?

From Dev

How to get all records from one table between dates of column in another table

From Dev

Get all records from table - EclipseLink

From Dev

Get all records from azure table storage

From Dev

SQLite: delete all records from table specified in another table

From Dev

SQLite: delete all records from table specified in another table

From Dev

SQL: All records from one table, and all records from another, including null

From Dev

Update all mysql records from one table into another

From Dev

How to use all records from another table as counting columns?

From Dev

SQL Query - select all from one table with matching records in another

From Dev

How to select all records from one table that do not exist in another table for certain condition in another table?

From Dev

Adding records to fields which get data from another table

From Dev

Get List ordered by number of records from another table using Linq

From Dev

Filter records from MYSQL Pivot table

From Dev

Get Records from Referenced Table "Minus" All The Records referenced from the Referencing Table

From Dev

How to get all records from one table and only records from joined table with criteria

From Dev

sql get all the records from table with some conditions

From Dev

how to get all the records from MySQL table in php web services

From Dev

How can I get all records from one table, even if there are no corresponding records in the JOINed table?

From Dev

how to select all records from one table and some from another table in cakephp 3.6

From Dev

Should I create another pivot table or just add another field on current pivot table that will be NULL for most records?

From Java

How to select all records from one table that do not exist in another table?

From Dev

Eliminate all duplicates according to a key + keep records from a table which are not in another table

From Dev

How to select all records from one table that do not exist in another table but return NULL in the record that do not exist

From Dev

Selecting records from a table and then selecting a count of records from another table

Related Related

  1. 1

    get records without records from another table

  2. 2

    Get all records from Model that do NOT have an entry in pivot table Laravel 5

  3. 3

    How to get 2 id from tow column and get all records from another table

  4. 4

    Get all data from pivot table

  5. 5

    How to get all records from one table and save it on another table by c# in winforms?

  6. 6

    How to get all records from one table between dates of column in another table

  7. 7

    Get all records from table - EclipseLink

  8. 8

    Get all records from azure table storage

  9. 9

    SQLite: delete all records from table specified in another table

  10. 10

    SQLite: delete all records from table specified in another table

  11. 11

    SQL: All records from one table, and all records from another, including null

  12. 12

    Update all mysql records from one table into another

  13. 13

    How to use all records from another table as counting columns?

  14. 14

    SQL Query - select all from one table with matching records in another

  15. 15

    How to select all records from one table that do not exist in another table for certain condition in another table?

  16. 16

    Adding records to fields which get data from another table

  17. 17

    Get List ordered by number of records from another table using Linq

  18. 18

    Filter records from MYSQL Pivot table

  19. 19

    Get Records from Referenced Table "Minus" All The Records referenced from the Referencing Table

  20. 20

    How to get all records from one table and only records from joined table with criteria

  21. 21

    sql get all the records from table with some conditions

  22. 22

    how to get all the records from MySQL table in php web services

  23. 23

    How can I get all records from one table, even if there are no corresponding records in the JOINed table?

  24. 24

    how to select all records from one table and some from another table in cakephp 3.6

  25. 25

    Should I create another pivot table or just add another field on current pivot table that will be NULL for most records?

  26. 26

    How to select all records from one table that do not exist in another table?

  27. 27

    Eliminate all duplicates according to a key + keep records from a table which are not in another table

  28. 28

    How to select all records from one table that do not exist in another table but return NULL in the record that do not exist

  29. 29

    Selecting records from a table and then selecting a count of records from another table

HotTag

Archive