How to get the Hierarchy recursively in laravel - php

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.

Related

Create App Quiz Using Laravel 7 and Mysql

i'm trying to create a simple Quiz App with Laravel and Mysql .
the idea is , i'm stucked on the relattions between questions/options .
first i create a table called "quiz"
ID name
1 Brazil
2 EUA
3 Japan
a table called "questions"
ID quiz_id name
1 1 President ?
2 1 Food?
3 1 Music?
4 1 Maiority?
and a table called "options"
ID question_id name
1 1 lula
2 1 bolsonaro
3 2 rice
4 2 beans
5 3 samba
6 3 rock
7 4 black
8 4 white
like image below , i receive the questions related with quiz . my function on my HomeController looks like this .
public function index(){
$quiz = Quiz::where('id',1)->with('questions')->get();
$array = array('quiz' =>$quiz);
return $array;
}
and my model Quiz have this relationship
public function questions(){
return $this->hasMany('App\quizquestion', 'quiz_id');
}
but i wanna make a relation of questions with options .
i'm thinking that must be another sub object inside questions . like a thirty leve .
Example :
quiz
questions
options.
because i wanna call this on my blade template , actualy i 'm calling this way
<body>
#foreach($quiz as $item)
<h1>{{$item->name}}</h1>
#foreach($item->questions as $quest)
<p>{{$quest->question}}</p>
#endforeach
#endforeach
</body>
that return me :
Brazil
President ?
Food ?
Music ?
Maiority?
I think you are looking for nested relationship query.
I assume you already setup the questions-options relationship. Here is a simplified query:
Quiz::with(['questions', 'questions.options'])->get();
or simpler (credits to #lagbox) :
Quiz::with('questions.options')->get();

Problem with getting data from query in Laravel

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();

Get the latest record of a particular id in a table

ScholarCard
scholar_card_id grade scholar_id
1 grade-1 1
2 grade-1 2
3 grade-2 1
4 grade-2 2
5 grade-3 1
$scholar_cards = ScholarCard::where('scholar_id','=',$scholar_id)->orderBy('scholar_card_id','DESC')->get();
I have a table for a card of grades and i want to display the latest created card.
Like for example for scholar_id 1,there are three 1 on the column.How do you display only
the scholar_id 1 with the latest or last creation.This table has only few record for simplicity.
You could use first() function. Try this:
$scholar_card = ScholarCard::where('scholar_id','=',$scholar_id)
->orderBy('scholar_card_id','DESC')
->first();

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.

Avoiding duplicates Database records in a datamapper code igniter

My Question is simply how to insert data into the join table(user_book) without inserting a duplicate entry into the main table(book).
Mainly consider we have tree table below
id Users id Books id Users Books
1 Sam 1 B1 1 1 1
2 Ben 2 B2 2 1 3
3 Mike 3 B3 3 3 3
4 2 2
But the problem is when I am inserting a new book(ex Mike like B3) that exist in the Books table. The duplicate will appear in the books table and the above table will be like:
id Users id Books id Users Books
1 Sam 1 B1 1 1 1
2 Ben 2 B2 2 1 3
3 Mike 3 B3 3 3 4
4 B3 4 2 2
Is that make sense now what I am trying to resolve? Or maybe I can't have a unique list of books at all?
if I am going to insert
Given the database
users and books
users<--->books are related by users_books
we need to make sure that when we insert a book record to the db it is not a duplicate. Further, if the book exist, a relation will be inserted and not the book record . How can one do that?
One approach is to say
$m=new Book();
$m->where('id_book', $data['user_booklist'][$i]['id'])->get();
$u = new User();
$u -> where('id_user', $data['user_profile']['id'])-> get();
if(!$u->where_related($m))
{
$m -> name = $data['user_booklist'][$i]['name'];
$m -> id_book = $data['user_booklist'][$i]['id'];
$m -> save($u);
}
if (book->exist) in the "books" table
then check the relation and see if there is a relationship between users and books and if there is then don't insert it. Further do I need to change any thing in the mysql db structure to avoid this.
The above code is not working but should give an idea what I am talking about.
Update:
In summary, if two users liked the same book, I just want to insert a record(book) into the join table (users_books) and while not to creating a new record in the table. Using the unique sql tag did not work since it keeps unique list but it prevents from inserting the multiple relationships into the join table users_books.
You could do:
$b = new book();
$b->where('id_book', $data['user_booklist'][$i]['id'])->get();
// I believe you can do this as well, but I'm not sure:
// $b->where('id', $data['user_booklist'][$i]['id'])->get();
$u = new user();
$u -> where('id_user', $data['user_profile']['id'])-> get();
if( $b->exists() && $u->exists() )
{
// This saves the relationship between the book and the user
$b->save($u);
}
else
{
// Could not find the book or the user
}
You should check out the datamapper-manual on saving relationships.

Categories