How to clone data that has self relational/ self reference in laravel - laravel

How to replicate/clone all data in a table that has self-relation in Laravel?
I have this table:
members table
id
member_id
company_id
name
1
null
23
John
2
1
23
James
3
1
23
Ken
4
null
23
Test
5
3
23
Max
EXISTING CODE
And I want to replicate all data that has company_id of 23 using
$members = Members::where['company_id' => 23]->get();
foreach ($members as $member) {
$duplicate = $member->replicate()->fill(['company_id' => 24);
$duplicate->save();
}
Results of the code
members table
id
member_id
company_id
name
1
null
23
John
2
1
23
James
3
1
23
Ken
4
null
23
Test
5
3
23
Max
6
null
24
John
7
1
24
James
8
1
24
Ken
9
null
24
Test
10
3
24
Max
But in that code, the replication of self-relation is wrong. How can I do it?
Expected result
members table
id
member_id
company_id
name
1
null
23
John
2
1
23
James
3
1
23
Ken
4
null
23
Test
5
3
23
Max
6
null
24
John
7
6
24
James
8
6
24
Ken
9
null
24
Test
10
8
24
Max
I'm sorry if my English is messed up, but I hope you get the idea of what I'm trying to do. Thank you!

Try this .You have two options to replicate
$members = Members::where(['company_id' => 23])->get();
foreach ($members as $member) {
$duplicate = $member->replicate();
$duplicate->company_id=24;
$duplicate->save();
}
});
or
$members = Members::where(['company_id' => 23])->get();
foreach ($members as $member) {
$duplicate = $member->replicate()->fill(['company_id'=>24]);
$duplicate->save();
}
});
Updated
$members = Members::where(['company_id' => 23])->get();
$tempIdMap=[];
foreach ($members as $key=>$member) {
if($key==0){
$tempIdMap[$member->id]=$member->member_id;
}
$duplicate = $member->replicate()->fill(['company_id'=>24,'member_id'=>isset( $tempIdMap[$member->member_id])? $tempIdMap[$member->member_id]:null]);
$duplicate->save();
$tempIdMap[$member->id]=$duplicate->id;
}

Related

Using Powershell to get third table from a website

I am trying to scrape the Team Statistics table from https://www.hockey-reference.com/leagues/NHL_2020.html , but am not getting the full expected output.
My code:
#get the data
$data = Invoke-WebRequest $url
#get the first table
$table = $data.ParsedHtml.getElementsByTagName("table") | Select -skip 2 | Select -First 1 |
#get the rows
$rows = $table.rows
#get table headers
$headers = $rows.item(1).children | select -ExpandProperty InnerText
#count number of rows
$NumOfRows = $rows | Measure-Object
#Manually injecting TeamName
$headers = #($headers[0];'TeamName';$headers[1..($headers.Length-1)])
#enumerate the remaining rows (skipping the header row) and create a custom object
$out = for ($i=2;$i -lt $NumofRows.Count;$i++) {
#define an empty hashtable
$objHash=[ordered]#{}
#get the child rows
$rowdata = $rows.item($i).children | select -ExpandProperty InnerText
for ($j=0;$j -lt $headers.count;$j++) {
#add each row of data to the hash table using the corresponding
#table header value
$objHash.Add($headers[$j],$rowdata[$j])
} #for
#turn the hashtable into a custom object
[pscustomobject]$objHash
} #for
The output:
Special Teams Shot Data
------------- ---------
Rk AvAge
1 Washington Capitals
2 St. Louis Blues
3 Boston Bruins
4 Pittsburgh Penguins
5 Tampa Bay Lightning
6 New York Islanders
7 Colorado Avalanche
8 Columbus Blue Ja...
9 Carolina Hurricanes
10 Vancouver Canucks
11 Philadelphia Flyers
12 Dallas Stars
13 Edmonton Oilers
14 Toronto Maple Leafs
15 Florida Panthers
16 Vegas Golden Kni...
17 Arizona Coyotes
18 Calgary Flames
19 Winnipeg Jets
20 Chicago Blackhawks
21 Buffalo Sabres
22 Montreal Canadiens
23 Nashville Predators
24 New York Rangers
25 Minnesota Wild
26 San Jose Sharks
27 Anaheim Ducks
28 Ottawa Senators
29 New Jersey Devils
30 Los Angeles Kings
31 Detroit Red Wings
League Ave... 27.8
I believe my issue lies in what I am selecting, though I haven't been able to figure out how to select all the necessary parts for that specific table.
Ideally I'd like to get it looking near identical to what's on the website, but getting all the stats outputted is just as good.

Get Total count

I want to merge two columns(Sender and Receiver) and get the Transaction Type count then merge another table with using Sender_Receiver primary id.
Sender Receiver Type Amount Date
773787639 777611388 1 300 2/1/2019
773631898 776806843 4 450 8/20/2019
773761571 777019819 6 369 2/11/2019
774295511 777084440 34 1000 1/22/2019
774263079 776816905 45 678 6/27/2019
774386894 777202863 12 2678 2/10/2019
773671537 777545555 14 38934 9/29/2019
774288117 777035194 18 21 4/22/2019
774242382 777132939 21 1275 9/30/2019
774144715 777049859 30 6309 7/4/2019
773911674 776938987 10 3528 5/1/2019
773397863 777548054 15 35892 7/6/2019
776816905 772345091 6 1234 7/7/2019
777035194 775623065 4 453454 7/20/2019
Second Table
Mobile_number Age
773787639 34
773787632 23
774288117 65
I am try to get like this kind of table
Sender/Receiver Type_1 Type_4 Type_12...... Type_45 Age
773787639 3 2 0 0 23
773631898 1 0 1 2 56
773397863 2 2 0 0 65
772345091 1 1 0 3 32
Ok, I have seen your old question and you just need inner join in sub-query as following:
SELECT
SenderReceiver,
COUNT(CASE WHEN Type = 1 THEN 1 END) AS Type_1,
COUNT(CASE WHEN Type = 2 THEN 1 END) AS Type_2,
COUNT(CASE WHEN Type = 3 THEN 1 END) AS Type_3,
...
COUNT(CASE WHEN Type = 45 THEN 1 END) AS Type_45,
Age -- changes here
FROM
( SELECT sr.SenderReceiver, sr.Type, st.Age from -- changes here
(SELECT Sender AS SenderReceiver, Type FROM yourTable
UNION ALL
SELECT Receiver, Type FROM yourTable) sr
join <second_table> st on st.Mobile_number = sr.SenderReceiver -- changes here
) t
GROUP BY
SenderReceiver,
Age; -- changes here
Changes done in your previous query are marked with comments -- changes here.
Please replace the name of the <second_table> with the original name of the table.
Cheers!!

How to get and display last updated field id in Laravel

I have a table total_count with the following structure:
id studid month year acls_id total_p total_a
1 30 08 2015 12 5 2
2 35 08 2015 12 5 2
3 52 08 2015 12 5 2
4 53 08 2015 12 5 2
5 54 08 2015 12 5 2
6 55 08 2015 12 5 2
7 30 09 2015 12 3 0
In my view page, this table displays a list of all studid with their details.
Here is my edit method in controller.php:
foreach ($student as $student) {
$queryt= DB::table($wys_attendence_table)->where('studid', $student->id)
->where('adate', $date_exploded[0])
->where('amonth', $date_exploded[1])
->where('ayear', $date_exploded[2])
->update(array(
'attendence'=>Input::get($student->id),
));
}
My edit function works.
$lastUpdatedId= $queryt->id;
$lastUpdatedStudid= $queryt->studid;
var_dump($lastUpdatedId);
var_dump($lastUpdatedStudid);
Does not work..
How to get and display updated field id and studid value.
for eg: if I am editing studid=30 and studid=52,
I want to display id=1,3 and studid = 30,52.

not getting output in group by using linq query

My table is:
Student_answer_Master:
Stud_ans_id QuestionId OptionId iscorrect
1 25 1 1
2 26 1 0
3 27 2 1
4 27 3 1
5 27 4 1
6 28 1 1
8 29 1 0
9 29 2 1
10 29 3 1
(my options are single choice and multiple choice(checkbox)).
now what i want to get total count of only those question which are correct.
1-indicate correct
0-indicate incorrect.
i want output:3(for questionid 25,27,28)
this is my linq query:
var data = (from temp in context.Student_Answer_Master
where temp.isCorrect == '1'
group temp by temp.Question_Id into g
select new {g.Key}).Count();
but here count is coming 0.
can any one tell me what is wrong with my query???
Try this this should work.
var data = (from temp in s
group temp by temp.Question_Id into g
select new { questionid = g.Key, minIscorrect = g.Min(x => x.isCorrect) }).Where(y => y.minIscorrect != 0).Count();
There are 2 things that looks wrong to me in your Query.
comparing an int to a char temp.isCorrect == '1' if you make it temp.isCorrect == 1, you will still get 4 as the count because you are eliminating the row that has wrong answer for Question id 29.
once you get the group, you need to check if there are any wrong answers for the question
if yes eliminate that and then put a count

COUNT returning NULL, should return 0

I have a simple query for the table:
Person id Organization id employee_nam age busines_group_id
123 Zuyo 10 John 30 81
2457 Zuyo 10 Geet 69 81
56 Ghiya 12 paul 20 81
frei 13 81
SELECT
COUNT(DISTINCT ped.person_id)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
SELECT
NVL(COUNT(DISTINCT ped.person_id), 0)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
p_org_id is the parameter which I am passing which can be 10, 12, or 13.
COUNT returns 2 for id 10. 1 for id 12. but is returning NULL for id 13.
I want 0 to be returned in this case.
NVL and CASE are also not working.
try MAX:
nvl(max(count(DISTINCT ped.person_id)),0)

Resources