J
J
javanub2018-10-29 22:48:12
Laravel
javanub, 2018-10-29 22:48:12

How can I display articles with a category, tags with such tables?

post table

+----+---------+-----------------+
| id |  title  |      text       |
+----+---------+-----------------+
|  1 | Title 1 | This is example |
|  2 | Title 2 | This is example |
|  3 | Title 3 | This is example |
+----+---------+-----------------+

Tags table
+----+--------+
| id |  tag   |
+----+--------+
|  1 | jQuery |
|  2 | PHP    |
|  3 | Stack  |
+----+--------+

category table
+----+------------+
| id |  category  |
+----+------------+
|  1 | Category 1 |
|  2 | Category 2 |
|  3 | Category 3 |
+----+------------+

Post tags relation table (similar to category)
+---------+--------+
| post_id | tag_id |
+---------+--------+
|       1 |      1 |
|       1 |      2 |
|       2 |      3 |
+---------+--------+

I would like to get this result, how can I do it?
+---------+------------------+--------------------+------------+
|  Title  |       Text       |        Tag        | Category |
+---------+------------------+--------------------+------------+
| Title 1 | This is example  | jQuery, PHP        | Category 2 |
| Title 2 | This is example  | Stack              | Category 1 |
| Title 3 | This is example  | jQuery, PHP, Stack | Category 1 |
+---------+------------------+--------------------+------------+

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
javanub, 2018-10-29
@javanub

I decided
dd(\App\Post::with('category', 'tag')->get());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question