Problem with getting data from query in Laravel - php

I have two tables
field_values (with some data)
id field_id value label sort
1 1 1 Men 1
2 1 2 Women 2
3 2 3 Relationship 1
4 2 4 Chat 2
5 2 5 Friendship 3
user_interests (with some data)
user_id field_id value_id
1 1 1
1 2 4
1 2 5
I am trying to write a query where I will get user with id 1 and have field_id 2 and to be able to echo in my blade value_id 4 and 5 but not to echo those ids but to echo value of 'label' column that corresponds to value_id form user_interests table in this case 4,5 thus Chat, Friendship from field_values table in this example. Here is what I tried but I get array of six elements which are Relationship, Chat, Friendship x2. Any help is appreciated.
query:
public static function queryFunction($userId)
{
$results = DB::table('user_interests as uin')
->select(DB::raw("
fv.*,
uin.field_id, uin.value_id
"))
->join('field_values as fv', 'fv.field_id', '=', 'uin.field_id')
->where('uin.field_id', 2)
->where('uin.user_id', $userId)
->get();
dd($results);
return $results;
}

What about 2 clear steps, without join:
$user_interests = DB::table('user_interests')->select('value_id')->where('field_id', 2)->where('user_id', $userId)->get();
From this take values as array ($user_interests_values)
and than
$results = DB::table('field_values')->whereIn('value', $user_interests_values)->get();

Related

Finding the most repeating id by laravel eloquent group

$device = SalesItem::where('type', '=', 1)->get()->groupBy('product_id');
There is a list of products in the database. Here I am storing the product id. The same product can be idsi. I want to get the id data of the product with the same id at most.
Sample:
ID type product_id
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
6 1 2
7 1 2
8 1 1
9 1 1
0 1 1
output: 1
here I want the output to give product id 1. I couldn't find what to do after group by. Can you guide me on this?
I don't think there is an elegant way to do this in, but this should work for you:
Note: I'm unsure of your table name.
$device = DB::table('sales_item')
->select('product_id', DB::raw('COUNT(product_id) AS magnitude'))
->where('type', 1)
->groupBy('product_id)
->orderBy('magnitude', 'DESC')
->limit(1);
It goes into the table and selected the id, type, and the COUNT() of product_id. Which it then groups by the product_id and orders by the count('product_id') from high to low.
->limit(1) is used to only select the first (read highest) value.

How to get the Hierarchy recursively in laravel

I have 2 tables i.e. users and user_managers, with the following structure and data:
users
id
name
employee_number
1
Employee One
ACB1234
2
Employee Two
XYZ1234
3
Employee Three
EFG1234
4
Employee Four
HIJ1234
user_managers
id
user_id
manager_of_user_id
1
1
2
2
2
3
3
4
1
4
5
4
I want a recursive function to get the complete chain of the managers. For example, if query for user_id = 4, I should get the following result:
result
user_id
name
employee_id
comments
1
Employee One
ABC1234
user managed by 4
2
Employee Two
XYZ1234
user managed by 1
3
Employee Three
EFG1234
managd by user 2
5
Employee Five
JKL1234
manager of user 4
The table above is just for the clarification, I am looking for a recursive function to get the above result.
I have tried the below solution but it is working for only 1 level.
public static function getCompleteChain(User $user)
{
$managerUsers = $user->managers;
foreach ($managerUsers as $manager) {
$manager->user = User::where('employee_id', $manager->manager_of_user_id)->first();
self::getCompleteChain($manager->user);
}
return $managerUsers;
}
Thanks in advance.

How to fetch all values of a column based on value of another column in same table?

I got the following structure:
admin_id || country_id
---------------------------------
1 2
5 1
1 2
2 3
5 62
1 1
3 62
How to fetch all values by taking the $_SESSION['admin']['id'], finding the country_id of that admin and getting all other admin_id and country_id that are the same of the session admin?
So, lets say the currently logged in admin has id = 5 , that means the admin_id: 5 has two country_id: 1 and 62. I want to take all rows that have country_id: 1 and 62.
It should return this:
admin_id || country_id
------------------------
5 1
5 62
1 1
3 62
How can I do this in one sql query?
You can use a where clause for filtering on the admin_id or the country_id:
select t.*
from t
where t.admin_id = 5 or
t.country_id in (select t2.country_id from t t2 where t2.admin_id = 5);
SELECT t1.*
FROM table t1
JOIN table t2 USING (country_id)
WHERE t2.admin_id = 5
fiddle

Calculation table2 data and updated to table1 LAravel

Table1
id name calculated_rating
1 xyz 2
2 abc 4.5
3 zzz 1
4 ddd 3
5 eee 2
Table2
id f_id rating
1 1 3
2 2 4
3 2 5
4 3 1
5 1 2
6 4 3
7 5 2
I have two tables one is Table1 and other is Table2
In table2 f_id that is foreign_key Table1 id is primary_key
Now Table2 has rating I want to added the calculate average rating in Table1 whenever Table2 increase rating it calculate average and update into Table1 calculated_rating field
How to achieve this in laravel
Whenever you add records to table2 like following you need to update current_rating value in table1.
Example,
id = 7
f_id = 5
rating = 2
You can create model for each table such as Table1 and Table2 using artisan command.
Now, Insert records like this:
$table2 = new Table2();
$table2->f_id = 5;
$table2->rating = 2;
$table2->save();
Now update new rating like this:
Table1::where('id',$table2->f_id)->update(['calculated_rating'=> Table2::where('f_id',$table2->f_id)->avg('rating')]);
Hope, this might help you.

mysql 2 tables updation/deletion based on condition

Hello friends I have 2 Mysql tables with 1:N relationship between category and category_Dates
Category:
ID Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
category_Dates:
ID CatID Date
1 1 01-Jan-15
2 1 01-Jul-15
3 2 01-Jan-15
4 2 01-Apr-15
5 2 01-Jul-15
6 2 01-Oct-15
based on the category frequency I am entering number of records automatically in category_date. Eg
When category frequency = quarterly, I am entering 4 records in category_date with ID of that category. And dates will be entered later.
I am little confused if in case on wants to edit the frequency from halfyearly to yearly. How to change number of records. Please help with your valuable suggestions. I am using laravel 4 framework with mysql
best way would be with 3rd table joining Dates and Categories. See little carefully ,you can see its actually Many to Many relationship (N to N) as 1 category can have multiple dates. and one date may be part of multiple categories, like say 01-Jan-15 is part of Category 1 and 2 as well.
So use
category table
id Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
date table
id Date
1 01-Jan-15
2 01-Apr-15
3 01-Jul-15
4 01-Oct-15
categories_dates table
ID CatID Date_id
1 1 1
2 1 3
3 2 1
4 2 2
5 2 3
6 2 4
If you change the frequency in Category table, retrieve the update category_id,
delete all from category_dates where CatId=category_id then insert the new entries in category_Dates.
Hope this help.
I assume your models are Category and CategoryDates.
let's update category id 1 from Half-yearlyto to Quarterly
$query = Category::find(1);
$query -> Frequency = 'Quarterly';
$query -> save();
return $query -> id;
in the CategoryDates model you would delete the catID = 1 and insert new data
$catID = 1;
$query = CategoryModel::where('CatId',$catId) -> delete();
$data = ['CatId' => $catID,'date' => 01-Jan-15, ....];
CategoryModel::create($data);
of course assuming that you would return the newly updated category id to your controller and call a funtionn to do the update in your CategoryModel.
Hope this help.

Categories