How do I Join table with multiple id in same column? - laravel

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

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.

Related

Laravel Eloquent join if column null

I have two tables users/invoices in 1:n relation. When someone creats an invoice it saves the user_id. Also it's possible (optional) to set a manager (column: project_management_user_id). If there is no manager set (NULL), I want to get the user, who created the record. So for now I have this query. It gives me all invoices with the manager (but not that ones with manager = null.
$query = Invoice::join('users', 'invoices.project_management_user_id', '=', 'users.id')
->select('users.name as name', 'users.color as color', DB::raw("count(invoices.id) as count"))
->get();
Is it possible to add some if-clause like "if project_management_user_id is null, take the user_id"? I read about the whereNull() function, but it filters my whole query
Use leftJoin instead of join in the query.

laravel 8 use orderBy and withMax for order tables with another table

i want to orderby products with max price from details table.
products one to many relation with details table.
in laravel documentation we have aggregate methods like withMax ,withMin and withCount
. These methods will place a {relation}_{function}_{column} attribute on your resulting models.
now we only need to orderBy with this results.
$products = Product::withMax('details', 'price')
->orderBy('details_max_price' , 'desc')
->paginate(15);

How to count for a substring over two tables in codeigniter

I have two tables
table1
id|name|next_field
table2
id|id_of_table1|whatelse
I do a msql query to get all entries of table1 and the number of entries in table2 who has table2.id_of_table1 = table1.id
This is my query - it works fine.
$select =array('table1.*', 'COUNT(table2.id) AS `my_count_result`',);
$this->db->select($select);
if($id!=false){ $this->db->where('id',$id); }
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't1.id = t2.id_of_table1');
return $this->db->get()->result_array();
Now I have another field which has coma-separated data
table1.next_field = info1, info2, next,...
Now I want to check in the same way like the first query how often for example "info2" is as a part inside the table1.next_field
Is it possible, how?
After all i decide to change my database structure to make the work and handle much easier.

How to use order by query both with current table column and relationship table column in laravel?

i am trying to fetch record with order by query but situation is this that i have to use order by both on current table column and relationship table column in laravel . I tried this
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with(['consignment_run_sheet' => function ($query) {
$query->orderBy('run_sheet_id');
}])->orderBy('delivery_date', 'DESC')->get();
$deliveryRuns = DeliveryRun::all();
How I can achieve it?
it will order just the relation items in this way. solution is use subquery or joins. useing Subquery is like this if assume the modal for consignment_run_sheet relation is ConsignmentRunSheet and the relation is belongsTo:
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with('consignment_run_sheet')
->orderBy(ConsignmentRunSheet::select('delivery_date')
->whereColumn('consignments.id', 'consignment_run_sheet.consignment_id'), 'DESC')
->get()
source:
https://reinink.ca/articles/ordering-database-queries-by-relationship-columns-in-laravel

join using eloquent to not take common column from joined table

I'm joining two tables that both have tag_id (i.e. album.tag_id and photo.tag_id)
function GetJoined()
{
return Album::join('photos', 'photos.tag_id', '=', 'albums.tag_id')
}
but I don't want that resulting query to keep reminding me that this was a join from two tables
GetJoined()->where('tag_id', 1);
will be all like
'tag_id' in where clause is ambiguous
Any suggestions?

Resources