Answer the question
In order to leave comments, you need to log in
How to get related data from DB (mysql) in one array element?
Guys, good afternoon!
To be honest, I don’t even know how to correctly formulate a question in order to google it.
In general, the situation is as follows.
There are 3 tables
1) doc_table
2) types
3) docs_types
1st table with documents 2nd table with document
types
3rd table is the connection of these 2
Here is my query to the database
$dataDb = DB::table('doc_table')->join('docs_types', function($join){
$join->on('docs_types.doc_id', '=', 'id')->where('id', '=', 1);
})->where('status', 1)->get();
Answer the question
In order to leave comments, you need to log in
start with this, it might help
SELECT dta.id, dta.doc_name, dta.doc_title, dta.link, dta.status, GROUP_CONCAT(DISTINCT dty.doc_id ORDER BY dty.doc_id ASC SEPARATOR ', ') AS type_id
FROM doc_table dta
INNER JOIN docs_types dty ON dta.id=dty.doc_id
GROUP BY dta.id;
SELECT dta.id, dta.doc_name, dta.doc_title, dta.link, dta.status, GROUP_CONCAT(DISTINCT t.type_name ORDER BY t.id ASC SEPARATOR ', ') AS type_name
FROM doc_table dta
INNER JOIN docs_types dty ON dta.id=dty.doc_id
INNER JOIN types t ON t.id=dty.type_id
GROUP BY dta.id;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question