Laravel Eloquent query order hierarchically - laravel

Need some help in getting a collection, using Laravel Eloquent, from a table but ordered by the parent_id to achieve, as an example, the following hierarchy:
Africa
Eastern Africa
Middle Africa
Northern Africa
Asia
Central Asia
Eastern Asia
(...) etc.
The below table is what I have and it's ordered by parent_id only.
What I'm trying to achieve is the parent > children ordering. Thanks in advance for any help.
+----+---------------------------------+-----------+
| id | region_common_name | parent_id |
+----+---------------------------------+-----------+
| 1 | Global | 0 |
| 2 | Africa | 1 |
| 14 | Americas | 1 |
| 33 | Antartica | 1 |
| 3 | Asia | 1 |
| 6 | Australasia | 1 |
| 4 | Europe | 1 |
| 10 | Eastern Africa | 2 |
| 11 | Middle Africa | 2 |
| 8 | Northern Africa | 2 |
| 12 | Southern Africa | 2 |
| 9 | Sub-Saharan Africa | 2 |
| 13 | Western Africa | 2 |
| 20 | Central Asia | 3 |
| 21 | Eastern Asia | 3 |
| 22 | South-eastern Asia | 3 |
| 23 | Southern Asia | 3 |
| 24 | Western Asia | 3 |
| 25 | Eastern Europe | 4 |
| 26 | Northern Europe | 4 |
| 27 | Southern Europe | 4 |
| 28 | Western Europe | 4 |
| 16 | Caribbean | 5 |
| 17 | Central America | 5 |
| 15 | Latin America and the Caribbean | 5 |
| 19 | Northern America | 5 |
| 29 | Australia and New Zealand | 6 |
| 30 | Melanesia | 6 |
| 31 | Micronesia | 6 |
| 32 | Polynesia | 6 |
| 5 | North America | 14 |
| 18 | South America | 14 |
| 7 | Southern America | 18 |
+----+---------------------------------+-----------+

You can order by multiple conditions but have to join on yourself.
Example in SQL
SELECT rp.region_common_name parent_name, r.region_common_name FROM regions r
INNER JOIN regions rp ON r.parent_id = rp.id
WHERE r.parent_id != 1 AND r.parent_id != 0
ORDER BY rp.region_common_name ACS, r.region_common_name ASC
So your result will look like this:
+---------------------------------+---------------------------------+
| parent_name | region_common_name |
+---------------------------------+---------------------------------+
| Africa | Eastern Africa |
| Africa | Middle Africa |
| Americas | North America |
| Americas | South America |
+---------------------------------+---------------------------------+
In eloquent you can use the QueryBuilder to generate your query.
It will look like this:
DB::table('regions as r')
->join('regions as rp', 'r.parent_id', '=', 'rp.id')
->where('r.parent_id', '!=', '0')
->where('r.parent_id', '!=', '1')
->orderBy('rp.region_common_name', 'asc')
->orderBy('r.region_common_name', 'asc')
->select('rp.region_common_name as parent_name', 'r.region_common_name as name');

Related

Create Trial Balance by using Laravel Eloquent

Hellow Team,
I would like to know how to extract the Trial balance from my journal entry data by using Laravel 9 Eloquent:
My Vouchers Table
| id |voucher_date| debit | credit| amount |
|----|------------|-------|-------|-----------|
| 1 | 2021-09-01 | 8 | 2 | 5000.000 |
| 6 | 2021-09-22 | 22 | 17 | 4750.000 |
| 8 | 2021-09-05 | 8 | 3 | 1485.000 |
| 9 | 2021-08-10 | 8 | 6 | 108.000 |
| 10 | 2021-07-07 | 8 | 23 | 98756.000 |
|11 | Etc. | ... |...... |........ |
Accounts table
| id | name | desc | status |
|----|-----------------------------------|-----------------------------------|--------|
| 1 | Assets | Current Assets | 1 |
| 2 | Stockholders equity | Stockholders or Owners equity | 1 |
| 3 | Liability | Liabilities related accounts | 1 |
| 4 | Operating Revenues | Operating Revenues | 1 |
| 5 | Operating Expenses | Operating Expenses | 1 |
| 6 | Non-operating revenues and gains | Non-operating revenues and gains | 1 |
| 7 | Non-operating expenses and losses | Non-operating expenses and losses | 1 |
| 8 | Etc. | More accounts....... | 1 |
My Desired output is like this: (Just an Example)
| Date | Account | Debit | Credit |
|------------|----------------------------------|---------:|----------:|
| 2021-09-01 | Stockholders equity | 0.00 | 5000.00 |
| 2021-09-05 | Liability | 0.00 | 1485.00 |
| 2021-08-10 | Non-operating revenues and gains | 0.00 | 108.00 |
| 2021-07-07 | Land | 0.00 | 98756.00 |
| 2021-02-25 | Land | 21564.00 | 0.00 |
| 2018-07-22 | Land | 3666.00 | 0.00 |
| 2018-05-14 | Non-operating revenues and gains | 0.00 | 489.00 |
| 2018-09-16 | Equipment | 692.00 | 0.00 |
| 2021-04-18 | Non-operating revenues and gains | 4986.00 | 0.00 |
| 2020-04-19 | Land | 4956.00 | 0.00 |
| 2019-03-15 | Buildings Asset | 0.00 | 4988.00 |
| 2019-12-04 | Inventory | 0.00 | 7946.00 |
| 2019-08-25 | Stockholders equity | 0.00 | 19449.00 |
| | | | |
| | Balance |36,990.00 |36,990.00 |
You need to assign a new foreign key to the the voucher table. And then you can simply apply the join to get the desired output. AS you mentioned that you are using debit and credit as foreign key how can they be used to uniquely identify the Vouchers table?

How to skip the "-- More --" in PostgreSQL pager?

When PostgreSQL spits a long output (eg. SELECT * FROM table_name for a table with 1000 lines), it will only show the first 50 or so lines.
There's a "-- More --" line at the bottom.
If you press enter, it will show the next line.
How can I skip the long PostgreSQL's output?
This is a PostgreSQL 12 run on cmd on Windows 10.
I tried:
\q on the --more-- line, then enter: just show the next line
\pset pager off is my current solution, although no ideal for showing table with 1000 row
dbname=# SELECT * FROM table_name;
id | first_name | last_name | email | gender | date_of_birth | country_of_birth
------+----------------+-------------------+---------------------------------------+--------+---------------+----------------------------------
1 | Erroll | Craisford | xxx#yyy.zzz | Male | 2019-05-28 | Indonesia
2 | Son | Smitherman | xxx#yyy.zzz | Male | 2019-02-16 | Indonesia
3 | Dion | Primo | xxx#yyy.zzz | Female | 2018-12-14 | Thailand
4 | Florette | Waldock | | Female | 2019-05-23 | Palestinian Territory
5 | Roderick | Stowte | xxx#yyy.zzz | Male | 2019-01-24 | Poland
6 | Hi | Kleeman | xxx#yyy.zzz | Male | 2019-01-26 | Indonesia
7 | Ethelind | Gard | xxx#yyy.zzz | Female | 2018-11-05 | France
8 | Bartel | Melhuish | | Male | 2019-02-18 | Vietnam
9 | Smith | Gavahan | xxx#yyy.zzz | Male | 2019-05-04 | Sweden
10 | Harmonia | Defrain | xxx#yyy.zzz | Female | 2018-12-17 | France
11 | Eulalie | Cuerdale | xxx#yyy.zzz | Female | 2019-05-09 | Angola
12 | Floria | Bernette | xxx#yyy.zzz | Female | 2019-07-07 | China
13 | Ruddy | Scargle | xxx#yyy.zzz | Male | 2019-08-27 | Norway
14 | Vinson | Capewell | xxx#yyy.zzz | Male | 2019-01-24 | Portugal
15 | Eben | Yellep | xxx#yyy.zzz | Male | 2019-03-12 | Mexico
16 | Yolande | Blaasch | xxx#yyy.zzz | Female | 2019-01-22 | Philippines
17 | Tiphani | Whitlow | xxx#yyy.zzz | Female | 2019-01-01 | New Zealand
18 | Alvina | Carne | xxx#yyy.zzz | Female | 2019-03-01 | Peru
19 | Peg | Hains | xxx#yyy.zzz | Female | 2019-02-22 | Indonesia
20 | Arlana | Sibson | xxx#yyy.zzz | Female | 2019-06-15 | Niger
21 | Rabi | Slimme | xxx#yyy.zzz | Male | 2019-03-03 | Belarus
22 | Marianna | Gouthier | | Female | 2019-05-06 | Sweden
-- More --
Just press q.
For more options see man page http://man7.org/linux/man-pages/man1/more.1.html#COMMANDS

Pivot Table in Hive and Create Multiple Columns for Unique Combinations

I want to pivot the following table
| ID | Code | date | qty |
| 1 | A | 1/1/19 | 11 |
| 1 | A | 2/1/19 | 12 |
| 2 | B | 1/1/19 | 13 |
| 2 | B | 2/1/19 | 14 |
| 3 | C | 1/1/19 | 15 |
| 3 | C | 3/1/19 | 16 |
into
| ID | Code | mth_1(1/1/19) | mth_2(2/1/19) | mth_3(3/1/19) |
| 1 | A | 11 | 12 | 0 |
| 2 | B | 13 | 14 | 0 |
| 3 | C | 15 | 0 | 16 |
I am new to hive, i am not sure how to implement it.
NOTE: I don't want to do mapping because my month values change over time.

Get the best route between two dirrent paths in wp8

I have created a bus transit application for a city in windows phone 8. I have stored all my routes and bus stops in a separate table in the database.
My bus stop table contains:
stop_id | stop_name | latitude | longitude | status | alias_name
---------------------------------------------------------------------------------------
1736 | Atlas Company | 18.629243 | 73.833814 | Active | Centurenca Corner
1737 | Atlas company | 18.629243 | 73.833814 | Active |
681 | Atma Anand Dhyan Kendra | 18.600349 | 73.926251 | InActive |
My Routes Table contains
bus_id | bus_no | bus_source | bus_destination | days_of_week | total_distance estimated_time | source_stop_names | destination_stop_names | total_stops | source_trip_time | destination_trip_time | bus_status
source_stop_names contains
-----------------
1 | Swargate
2 | Parvati payatha
3 | Dandekar pul
4 | Pan mala Sinhgad Road
5 | Jal Shuddhikarn Kendra Sinhgad Road
6 | Ganesh mala
7 | Vitbhatti Sinhgad Road
8 | Vitthalwadi jakat naka
9 | Jaydeo nagar
10 | Rajaram pul
11 | Vitthalwadi Mandir Hingne
12 | Hingne rasta
13 | Anand nagar singhgad rd
14 | Manik Bag
15 | Indian hum company
16 | Wadgaon phata
17 | Patil colony
18 | Dhayari phata
19 | Sanas Vidyalaya
20 | Dangat wasti
21 | Gar mala
22 | Dhayarai gaon
23 | Raykar wasti
24 | Poultry farm singhgad road
25 | Dhayarigaon shala
26 | Chavan mala
27 | DSK Vishwa
I want to find the shortest path between two bus stops not connected by a bus route, and show the number of routes and buses the user has to take while travelling from one point to another, like google maps does.
I have used default map control in windows phone 8.

MySQL equivalent of ORACLES rank()

Oracle has 2 functions - rank() and dense_rank() - which i've found very useful for some applications. I am doing something in mysql now and was wondering if they have something equivalent to those?
Nothing directly equivalent, but you can fake it with some (not terribly efficient) self-joins. Some sample code from a collection of MySQL query howtos:
SELECT v1.name, v1.votes, COUNT(v2.votes) AS Rank
FROM votes v1
JOIN votes v2 ON v1.votes < v2.votes OR (v1.votes=v2.votes and v1.name = v2.name)
GROUP BY v1.name, v1.votes
ORDER BY v1.votes DESC, v1.name DESC;
+-------+-------+------+
| name | votes | Rank |
+-------+-------+------+
| Green | 50 | 1 |
| Black | 40 | 2 |
| White | 20 | 3 |
| Brown | 20 | 3 |
| Jones | 15 | 5 |
| Smith | 10 | 6 |
+-------+-------+------+
how about this "dense_rank implement" in MySQL
CREATE TABLE `person` (
`id` int(11) DEFAULT NULL,
`first_name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`gender` char(1) DEFAULT NULL);
INSERT INTO `person` VALUES
(1,'Bob',25,'M'),
(2,'Jane',20,'F'),
(3,'Jack',30,'M'),
(4,'Bill',32,'M'),
(5,'Nick',22,'M'),
(6,'Kathy',18,'F'),
(7,'Steve',36,'M'),
(8,'Anne',25,'F'),
(9,'Mike',25,'M');
the data before dense_rank() like this
mysql> select * from person;
+------+------------+------+--------+
| id | first_name | age | gender |
+------+------------+------+--------+
| 1 | Bob | 25 | M |
| 2 | Jane | 20 | F |
| 3 | Jack | 30 | M |
| 4 | Bill | 32 | M |
| 5 | Nick | 22 | M |
| 6 | Kathy | 18 | F |
| 7 | Steve | 36 | M |
| 8 | Anne | 25 | F |
| 9 | Mike | 25 | M |
+------+------------+------+--------+
9 rows in set (0.00 sec)
the data after dense_rank() like this,including "partition by" function
+------------+--------+------+------+
| first_name | gender | age | rank |
+------------+--------+------+------+
| Anne | F | 25 | 1 |
| Jane | F | 20 | 2 |
| Kathy | F | 18 | 3 |
| Steve | M | 36 | 1 |
| Bill | M | 32 | 2 |
| Jack | M | 30 | 3 |
| Mike | M | 25 | 4 |
| Bob | M | 25 | 4 |
| Nick | M | 22 | 6 |
+------------+--------+------+------+
9 rows in set (0.00 sec)
the query statement is
select first_name,t1.gender,age,FIND_IN_SET(age,t1.age_set) as rank from person t2,
(select gender,group_concat(age order by age desc) as age_set from person group by gender) t1
where t1.gender=t2.gender
order by t1.gender,rank

Resources