How to get three tables data in laravel7 - laravel-7

i have three table country, state, customer_contacts.
I want display all customer details into list structure. But in my customer table only id save of country and state respectively.
Table structure as follows :
Country
id name
1 India
2 Canada
State
id name country_id
1 Mumbai 1
2 Delhi 1
3 abc 2
4 xyz 2
Customer_contact
id c_name country_id state_id
1 abcdee 1 2
2 xyzerr 1 1
3 extraa 2 3
4 newsss 2 4
i want to fetch customer name with country name and state name.
I am using below query to fetch data but getting only customer contact data how to fetch name uaing any query or relationship.
$data = CustomerContact::latest()->get();
If any solution then let me know

You 2 have possibillities here:
First, you can use the relationships:
CustomerContact model
country()
{
return $this->belongsTo(Country::class);
}
Country model
state()
{
return $this->belongsTo(State::class);
}
Use the relationships like this:
$data = CustomerContact::with('country.state')->latest()->get();
Second option would be to use join():
CustomerContact
::join('countries', 'countries.id', 'customer_contact.country_id')
->join('states', 'states.id', 'countries.state_id')
->latest()
->get();

Related

Replacing Null Values with Mean Value of the Column in Grid DB

So, I was working with GridDB NodeJs Connector, I know the query to find out the null values which shows the records/rows:
SELECT * FROM employees where employee_salary = NaN;
But I want to replace the null values of the column with the mean value of the column, in order to maintain the data consistency for data analysis. How do I do that in GridDB?
The Employee table looks like the following:
employee_id employee_salary first_name department
---------------+---------------+--------------+--------------
0 John Sales
1 60000 Lisa Development
2 45000 Richard Sales
3 50000 Lina Marketing
4 55000 Anderson Development

I have a employees table and a tasks table (posgresql). I need to assign many tasks to one employee

I am trying to make a join table, with two columns, employee id and task ids. I select 5 tasks(Ids) and one employee on the client side, send an array of 5 task-Ids and an employee identifier to the server. But these 5 elements are not distributed in different lines.
I want such a result:
Employee_id Task_id
1 4
1 3
1 2
1 1
1 5
Node.js with Express.
Pass the task_list as an array then use the unroll function to break it into individual id.
insert into employee_task(empployee_id, task_id)
select 1, unnest(array[4,3,2,1,5]);

create database from with 2 distinct database according to customer name in python

Table A: Product Attributes
This table contains two columns; the first one is a unique product ID represented by an integer, the second is a string containing a collection of attributes assigned to that product.
product tags
100 chocolate, sprinkles
101 chocolate, sprinkles
102 glazed
Table B: Customer Attributes
The second table contains two columns as well; the first one is a string that contains a customer name, the second is an integer that contains a product number. The product IDs from column two are the same as the product IDs from column one of Table A.
customer product
A 100
A 101
B 101
C 100
C 102
B 101
A 100
C 102
Generated Table
I want to create a table matching this format, where the contents of the cells represent the count of occurrences of product attribute by customer.
customer chocolate sprinkles glazed
A ? ? ?
B ? ? ?
C ? ? ?
I want count instead of ?.
And I want to do this in python.
One more question: If the two starting tables were in a relational database or Hadoop cluster and each had 100 million rows, how might my approach change?

Want to get count and cityname for all the rows in a table in cassandra

Any other process to get all the count with the distinct values from a table in cassandra like a group function in MySQL or Oracle
Example following is a table with cities:
cityid cityname
1 Lucknow
1 Lucknow
3 Delhi
4 Noida
5 Agra
5 Agra
5 Agra
I want the following output produced in cassandra:
count Cityname
2 Lucknow
1 Noida
1 Delhi
3 Agra
Function count(*) does not exist in cassandra. Alternativelly you can use counter https://docs.datastax.com/en/cql/3.1/cql/cql_using/use_counter_t.html.
create table cities(
cityname text,
count counter,
PRIMARY KEY(cityname)
);
For updating the table use:
UPDATE cities SET count = count + 1 WHERE cityname ='Lucknow';

Sub entities in solr

I'm using solr to index an entity which has indefinite number of related entities
Table 1
id name
1 | aa
2 | bb
3 | cc
Table 2
id field1 field2
1 | works in | New York
1 | likes to go to | Paris
As you see, each row represents an entity related to entity with id 1 and which value corresponds which matters.
How do I achieve this with Solr's data import handler?
I used SubEntity in data-config.xml and multiValued=true for field1 and field2, but the indexed document looks like
id 1
field1:[works in, likes to go to]
field2:[New York, Paris]
and the relationships between columns were completely lost. If one searches works in Paris he can also get entity 1. What should I do to maintain the relationships? Thanks a lot.
Schema Definition in schema.xml
id(type string)
name(type string)
worksIn (type string, multi value= true) - your choice if multi-value required or not
likesToGo (type string, multi value= true) - multivalue makes sense here as person is,most likely have more places to go, anyways your requirement
Sample docs after indexing
1,aa, worksIn[Newyork, New Jeysey], likesToGo[Paris, Moon]
2,bb, worksIn[Dallas], likesToGo[NewYork, Sun]
Querying
For "works in Paris", query is "worksIn:Paris".
You get doc with id 1
For "likes to go to sun", query is "likesToGo:sun".
You get doc with id 2

Resources