Sum of multiple table row - laravel

How can i get sum of all these counts?
{{ DB::table('A')->count()}}
{{ DB::table('B')->count()}}
{{ DB::table('C')->count()}}
{{ DB::table('D')->count()}}

// Your Tables Which You Know Them Counts
$tables = ['A', 'B'];
// Our Count Start From Zero
$count = 0;
// Count Each Table And Add Our Count
foreach ($tables as $table) {
$tableCount = DB::table($table)->count();
$count += $tableCount;
}
// Display Count With dd()
dd( $count );
// Or Display In View
return view('index', compact('count'));

Like this?:
{{ $a = DB::table('A')->count()}}
{{ $b = DB::table('B')->count()}}
{{ $c = DB::table('C')->count()}}
{{ $d = DB::table('D')->count()}}
{{ $sum = $a + $b + $c + $d }}
But you may want to send all these data from a controller method instead of doing it in a view.

Related

check data that is not in array with 2 value matches

I have 2 arrays and I want to check those arrays with 2 value matches,
if those 2 value not matches I want to insert into database.
Code I have tried :
$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
foreach($data1 as $d1)
foreach($data2 as $d2)
if($d1['reg_no'] != $d2['reg_no'] && $d1['name'] != $d2['name'])
//insert into database
// this not work because it will enter all data that not match
endif
endforeach
endforeach
you can use FirstOrNew Or FirstOrCreate in laravel.
like insert if not exist
$students = User::firstOrNew(['student_id' => $request->student_id, 'course_code' => $request->course_code]);
$students->foo = $request->foo;
$students->save();
$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
$data = array_uintersect($data1, $data2, "strcasecmp");
if($data['reg_no'] != $data1['reg_no'] && $data2['reg_no']) {
// insert
}

see value according to the array

I do a select and get several results in array but I need to get the correct value for each step and set up a condition.
$step = DB::table('records')->where('id_user',$userId)->get();
for($i = 0; $i < count($step); $i++)
{
echo $step[$i]->id_step;
}
Id_step returns me values for each step where on the blade I need to get and see if id_step = 1 is true id_step = 2 is true.
This for is returning me only one value and it has 3 records in the table.
First of all. After a select you get an instance of Eloquent\Collection
Not an array.
So that said to loop do this:
$steps = DB::table('records')->where('id_user',$userId)->get();
foreach($steps as $row) {
echo $row;
}
Since you are familiar with arrays do this:
$steps->toArray();
Now your result is an array
Working insert in view this code.
#for($i = 0; $i < count($step); $i++)
#if($step[$i]->id_step == 2)
working
#else
not working
#endif
#endfor

laravel eloquent union, join 2 columns in the same table

I have the following columns in the same table
Column 1
--------
1
2
3
Column 2
--------
4
5
6
I want it to be displayed like
Columns
--------
1
2
3
4
5
6
You can use unionAll
$first = DB::table('yourTable')
->select('column1 as res');
$result = DB::table('yourTable')
->select('column2 as res')
->unionAll($first)
->get();
You can make the Union of your data by taking out separately and merge it after. You can use union() as well as merge(), One thing to mention is that both data must have same skelton means structure.
use DB;
public function yourFunction(){
$data_1 = DB::table('table_name')->get();
$data_2 = DB::table('table_name')->get();
//your final data
$final_data = $data_1->union($data_2);
}
Assuming values are in the same row:
$data = DB::table('table_name')->select('column1', 'column2')->get();
$c = count($data);
if ($c) {
$col1 = [];
$col2 = [];
foreach ($data as $k => $d) {
$col1[] = $d->column1;
$col2[$c + $k] = $d->column2;
}
$result = array_merge($col1, $col2);
foreach ($result as $value) {
echo $value."\n";
}
} else {
echo "No data in table.";
}

how can I run for loop 4 times

I have an array that I have counted and it equals 4. That is what it should equal. Now I want the foreach loop inside the for loop to only run 4 times. As it stands, I am getting to many results. Below is my latest attempt that does not work.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i <= $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
$i++;
}
the networks array is populated from a WordPress widget that has a repeating field section. The section allows the user to set social media icons. I currently have 4 social media icons set. On the front end, the page shows every social media icon available even though there are only 4 set. So I am trying to get the nested foreach loop to only run 4 times.
Currently your loop is executing 5 times..and you want it to run only 4 times... So Change this condition...for($i = 0; $i <= $size; $i++) to either for($i = 0; $i <$size; $i++) or for($i = 1; $i <= $size; $i++) like i.e.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i < $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
}
I have something more interesting for you..
it's a widget parent with children!
You can change field to load new fields from other classes
here's the link:
https://github.com/hishamdalal/parent_widget

How I can get 0 index value in codeigniter?

Array ( [0] => 18 [1] => 1 )
how i can get only 0 index value?. I am using this code in codeigniter. Can any one help?. Is this possible with for each loop so it access all indexes but show only zero index?
foreach($m as $m)
{
echo $m->['0'];
}
If you want only first index then you don't need to foreach loop
Just write:
echo $m[0];
And if you want all index of array then:
foreach ($m as $key => $value) {
echo $key;
}
Try this:
print_r($m[0]);
or
foreach($m as $m)
{
echo $m['0'];
}
Following is the code to get value of index 0
//$array variable declared containing multiple values
$array=array('0' =>"first value",'1' =>"second value",'2' =>"third value" );
echo "<pre>";
print_r($array); // helps in printing the value key and value
//iterating through each values in the array
foreach ($array as $key => $value) {
if($key==0) //checks if key is 0
{
echo $value; //prints the value in key 0
}
}

Resources