Can we Put 3 database data in One Table - laravel

I am self learner and stuck at one point. I don't know is that possible or not. Please refer image of data get in array.
I have 3 database table "animalsell", "dairyincomes" and "othersell". I sucessfully able to get required data from this.
Now I want to put the same in one table in blade file (refer table image)
Controller file where i collect the data
$fiveincomedata = Dairyincome::select(
DB::raw('DATE_FORMAT(milksaledate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(milksaledate) as milksaledate')
)
->whereBetween('milksaledate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('milksaledate', 'desc')
->get()->toArray();
// Income from Animal Sell
$fiveanimalselldata = Animalsell::select(
DB::raw('DATE_FORMAT(selldate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(selldate) as selldate')
)
->whereBetween('selldate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('selldate', 'desc')
->get()->toArray();
// Income from Other Sell
$fiveotherselldata = Othersell::select(
DB::raw('DATE_FORMAT(saledate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(saledate) as saledate')
)
->whereBetween('saledate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('saledate', 'desc')
->get()->toArray();
Blade File
<div class="card-body">
<table id="incometable" class="table m-0 table table-bordered table-hover table" style="width:100%">
<thead>
<tr>
<th>Date</th>
<th>Amount (Milk Sale)</th>
<th>Amount (Animal Sale)</th>
<th>Amount (Other Sale)</th>
<th>Total Amount </th>
</tr>
</thead>
<tbody>
#foreach ($fiveincomedata as $fivei )
<tr>
<td>{{ $fivei->month_name }}</td>
<td>{{$fivei->totalamount}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Hope i explain my problem clearly and thanks for help in advance.

$a1=array("red","green");
$a2=array("blue","yellow");
array_merge($a1,$a2);
or
push the data into a common array.

You can use the merge() function.
For example:
$fiveincomedata = Dairyincome::all();
$fiveanimalselldata = Animalsell::all();
$fiveotherselldata = Othersell::all();
$merged = $fiveincomedata->merge($fiveanimalselldata);
$merged = $merged->merge($fiveotherselldata);
$array = $merged->toArray();
More information: https://laravel.com/docs/9.x/collections#method-merge

Related

fetch multiple records from a single table having same id

I am writing a function in laravel 5.8 in which I want to get sum of the user amount that lies within my selected range, I am using the two input fields in view which they get the price range "T0 and From" e.g if the user enters 100 and 1000, it should show the list of the users who has amount > 100 and amount < 1000. The problem is there are multiple records of the same users as shown below in the table.
I want to sum the amount of the users and show the user if their amount is within range
table name= deposits
<table>
<thead>
<tr>
<th>id</th>
<th>user_id</th>
<th>name</th>
<th>amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>12</td>
<td>ali</td>
<td>100</td>
</tr>
<tr>
<td>2</td>
<td>12</td>
<td>ali</td>
<td>800</td>
</tr>
<tr>
<td>3</td>
<td>12</td>
<td>ali</td>
<td>50</td>
</tr>
<tr>
<td>4</td>
<td>15</td>
<td>khan</td>
<td>1100</td>
</tr>
<tr>
<td>6</td>
<td>9</td>
<td>james</td>
<td>850</td>
</tr>
<tr>
<td>7</td>
<td>9</td>
<td>james</td>
<td>90</td>
</tr>
</tbody>
</table>
This behavior of your query is correct, to get what you need it is necessary to work on the query.
Assuming this fields in your deposits table: id, user_id, name, amount in according with data showing in the table.
You need to sum the amount grouping by user_id
Example query:
$deposit = Deposits::select(
'id',
'user_id',
'name',
'sum(amount)',
)->whereBetween('amount', [100, 1000])
->groupBy('user_id')->get();
DB::table('deposits')
->selectRaw('user_id, SUM(amount) as total_amount')
->where('amount', '>', '100')
->where('amount', '<', '1000')
->groupBy('user_id')
->get();
Result is:
[
{
"user_id":9,
"total_amount":850
},
{
"user_id":12,
"total_amount":800
}
]
app('db')
->table('users')
->select('name')
->selectRaw("SUM(amount) as total_amounts")
->havingBetween('total_amounts', [ 100, 1000 ])
->groupBy('user_id')
->get();
You can use the raw query for the sum per user and use the having between for the sum of amounts.

Laravel, trying to make a count with values from database

I need some help with Laravel, I'm trying to do a list, with information from my database, with a count, for example:
List image
How can I make to count how many times the same place appears in my list? Remembering: My place list comes from my DB, the place can variable. I can't make it count. Here is my code:
<table border=`1px` class="table-teste">
<tr>
<td><h3> Place</h3></td>
<td><h3> Count</h3></td>
</tr>
#foreach ($setor_data as $setor)
<tr>
<td>{{$setor->setor}}</td>
</tr>
#endforeach
</table>
And my Controller:
$setor_data = DB::table('atividades')
->where('local', $condominio)
->whereDate('date', '>=', $dtinicio)
->whereDate('date','<=',$dtok)
->orderBy("setor")
->select('setor')
->get();
User groupBy to count each item in list in the query, like following:
$setor_data = DB::table('atividades')
->where('local', $condominio)
->whereDate('date', '>=', $dtinicio)
->whereDate('date','<=',$dtok)
->orderBy("setor")
->select('setor', , DB::raw('count(*) as total'))
->get();
Then in the blade view:
#foreach ($setor_data as $setor)
<tr>
<td>{{$setor->setor}}</td>
<td>{{$setor->total}}</td>
</tr>
#endforeach
Hope, it helps...

How to showing data on blade with pass through 2 table FK

I have 3 table like users , data_users , practice
users : id , name ,dob ,email , etc . . .
data_users : id , user_id , number_exp , etc . .
practice : id , data_user_id , practice_number , etc ....
in 3 tables connect by relation belongsTo and and want to showing this .
i want to show data on table practice :
My controller :
public function show()
{
$practice= Practice::with('data_users')->get();
return view('admin.practice' ,['practice'=>$practice]);
}
my view
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>No Surat</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach ($practice as $i)
<tr>
<th scope="row">1</th>
<td>{{ $i->data_users->users->name}}</td>//i want to show name but i cant show this
<td>{{ $i->practice_number}}</td>
<td>{{ $i->practice_date}}</td>
<td>{{ $i->status}}</td>
</tr>
#endforeach
</tbody>
</table>
i have problem to showing Data 'NAME' form table users , and i just can showing id from data_users
but still error Trying to Get property
try this:
public function show()
{
$practice= Practice::with('data_users', 'data_users.users')->get();
return view('admin.practice' ,['practice'=>$practice]);
}
You are returning $practice collection from controller, trying to foreach $riwayat collection.

Invalid argument supplied for foreach() in Laravel

I have double foreach here. Here is my view
<table border="1">
<tr>
<th>ID</th>
<th>ISBN</th>
<th>Klasifikasi</th>
<th>Lokasi</th>
<th>Cp_Or</th>
<th>Tahun</th>
<th>ID_Master_Buku</th
<th>Jenis</th>
<th>Status</th>
<th>Tgl_Masuk</th>
<th>can_issue</th>
<th>ID</th>
<th>Edisi</th>
<th>Pengarang</th>
<th>Deskripsi</th>
<th>Penerbit</th>
<th>Judul</th>
<th>Jumlah_Buku</th>
<th>Bahasa</th>
<th>Gambar</th>
<th>Subjek</th>
<th>Topik</th>
</tr>
#foreach($bukus as $buku)
#foreach($buku->tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>
<td>{{$buku->Lokasi}}</td>
<td>{{$buku->Cp_Or}}</td>
<td>{{$buku->Tahun}}</td>
<td>{{$buku->ID_Master_Buku}}</td>
<td>{{$buku->Jenis}}</td>
<td>{{$buku->Status}}</td>
<td>{{$buku->Tgl_Masuk}}</td>
<td>{{$buku->can_issue}}</td>
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>
<td>{{$item->Penerbit}}</td>
<td>{{$item->Judul}}</td>
<td>{{$item->Jumlah_Buku}}</td>
<td>{{$item->Bahasa}}</td>
<td>{{$item->Gambar}}</td>
<td>{{$item->Subjek}}</td>
<td>{{$item->Topik}}</td>
#endforeach
</tr>
#endforeach </table>
It works in my other views. But here, I got an error
Invalid argument supplied for foreach()
Could you please help me? Thanks in advance
Sounds a lot like at least one $buku->tmbuku is null...
Try wrapping it in an #if:
#foreach($bukus as $buku)
#if($tmbuku = $buku->tmbuku
#foreach($tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>
<td>{{$buku->Lokasi}}</td>
<td>{{$buku->Cp_Or}}</td>
<td>{{$buku->Tahun}}</td>
<td>{{$buku->ID_Master_Buku}}</td>
<td>{{$buku->Jenis}}</td>
<td>{{$buku->Status}}</td>
<td>{{$buku->Tgl_Masuk}}</td>
<td>{{$buku->can_issue}}</td>
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>
<td>{{$item->Penerbit}}</td>
<td>{{$item->Judul}}</td>
<td>{{$item->Jumlah_Buku}}</td>
<td>{{$item->Bahasa}}</td>
<td>{{$item->Gambar}}</td>
<td>{{$item->Subjek}}</td>
<td>{{$item->Topik}}</td>
</tr>
#endforeach
#endif
#endforeach
Try checking if its null and items count before executing the second loop e.g.:
#foreach($bukus as $buku)
#if(!is_null( $buku->tmbuku ) && count( $buku->tmbuku ) )
#foreach($buku->tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>....
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>...
</tr>
#endforeach
#endif
#endforeach

Where and how is this data being changed?

The following code works fine, except that there is some "cleaning up" of the data that is taking place in one instance and not the other that I can't decipher.
I have a table (clientdocs) with a field 'from' that contains string data in the format:
Richard Robinson <richard#gmail.com>
In my controller, I have code that builds an array for a dropdown from this table:
// get clients
$sender_sel = array('Any Sender');
$senders = Clientdoc::orderBy('from', 'ASC')
->distinct()
->get( array('from') );
foreach($senders as $sender) {
$sender_sel[] = $sender->from;
}
I also retrieve all the data from the table and pass it to the view (along with the above array and other stuff):
return View::make('clientdocs/index')
->with('date_sel', $date_sel)
->with('sender_sel', $sender_sel)
->with('docs', Clientdoc::orderBy('date_emailed', 'ASC')->get());
Now, in my view:
I use $sender_sel as:
{{ Form::select('from', $sender_sel, '', array(
'class' => 'form-control'
)) }}
and it displays as expected:
"Richard Robinson <richard#gmail.com>"
and I also use data from the 2nd query (docs object):
<table class="table table-striped table-hover">
<tr>
<th>Date</th>
<th>From</th>
<th>Type</th>
</tr>
#foreach ($docs as $doc)
<?php $detail_url = 'clientdocs/' . $doc->id . '/edit' ; ?>
<tr>
<td>{{ $doc->from }}</td>
</tr>
#endforeach
</table>
Here's the kicker: $docs->from displays as
"Richard Robinson", without the "<richard#gmail.com>"
Any ideas why?
View source. It's still there, but < and > are special characters in HTML (as they're used in HTML tags).
Using triple brackets escapes output in Blade, so {{{ $doc->from }}} will fix it by turning it into < and >.

Resources