Reputation: 53
How do I apply join for this kind of table where document_id
is stored in array. I am aware about explode function but I really dont know how to use. I do not want to use for loop but I am expecting better idea to join this table. If I have to add something then I will add.
$row = DB::table('service_documents')->select('service_documents.document_id', 'service_documents.service_id')->get();
$idArr = explode(',', $row);
dd($idArr);
$documents = DB::table('service_documents')->select('service.name_np as serviceName',
'documents.name_np as documentName', 'service_documents.*')
->join('service', 'service_documents.service_id', '=', 'service.id')
->join('documents', 'service_documents.document_id', '=', 'documents.id')
->get();
This is my join query in laravel
Upvotes: 1
Views: 342
Reputation: 17206
The best solution (to have a performant query) is to add a new pivot table document_service_document
with service_document_id
and document_id
as fields.
Then populate that table using existing document_id field and adapt all the codes linked to that field to use the pivot table instead.
for that, and after, you can use manyToMany relation or simple join and get the results you need the easy way.
Upvotes: 3