Laravel Join and groupBy Cannot Show Column Data - php

$result = DB::table('students')
->join('cities', 'cities.id', '=', 'students.city_id' )
->select('cities.city_name as CityStudent', DB::raw('COUNT(students.id) as total'))
->groupBy('cities.id')->get();
view blade :
#foreach($result as $r)
<tr>
<td>{{ $r->CityStudent }}</td>
<td>{{ $r->total }}</td>
</tr>
#endforeach
I want to display the number of cities. And display the name of the city in the data.
Why does the code error and it doesn't work?

Related

How to i get gaji from table gaji when i choose grade

How to i get gaji from table gaji when i choose grade
if I choose grade A, the salary will be 1000000 automatically
Pegawai.blade.php
#php
$A = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
$B = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
$C = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
$D = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
#endphp
<tbody>
#foreach($ar_karyawan as $peg)
<tr>
<td>{{ $no++ }}</td>
<td>{{ $peg->nip }}</td>
<td>{{ $peg->nama }}</td>
<td>{{ $peg->gender }}</td>
<td>{{ $peg->tgl_lahir }}</td>
<td>{{ $peg->tgl_masuk }}</td>
<td>{{ $peg->grade }}</td>
</tr>
#endforeach
</tbody>
</table>
PegawaiController.php
public function index()
{
$ar_karyawan = DB::table('karyawan')
->join('gaji', 'gaji.id', '=', 'karyawan.gaji_id')
->select('karyawan.*', 'gaji.gaji AS gaji')
->get();
return view('pegawai.index', compact('ar_karyawan'));
}
table pegawai
https://i.imgur.com/Iy6ev8l.png
table gaji
https://i.imgur.com/MTxrJ0g.png
You wrote the code the wrong way. You wrote duplicate "->->"
Change the following code
from
$A = DB::table('employee')->join('salary','salary.id', '=', 'employee.salary_id')->->get('salary.salary');
to
$A = DB::table('employee')->join('salary','salary.id', '=', 'employee.salary_id')->get('salary.salary');

How to join two tables in Laravel

I'm trying to join two different tables to display in the foreach loop in my index.blade file
My Controller:
public function index()
{
$users = DB::table('accounts')
->join('users', 'accounts.user_id', '=', 'users.id')
->select('users.accno', 'accounts.*')
->first();
return view('accounts.index', compact('users'));
}
My blade:
#foreach($users as $user)
<tr>
<td>{{ $user->accno }}</td>
<td>{{ $user->balance }}</td>
<td>{{ $user->amt }}</td>
</tr>
#endforeach
My index.blade.php where I'm implementing the controller, the {{user->accno}} is from my user table and the rest is from accounts table
$users = DB::table('accounts')
->join('users', 'accounts.user_id', '=', 'users.id')
->select('users.accno', 'accounts.*')
->first();
In your controller, you are using for first(). So need to use foreach. Just write:
<tr>
<td>{{ $user->accno }}</td>
<td>{{ $user->balance }}</td>
<td>{{ $user->amt }}</td>
</tr>
Or you should use get() instead of first(). But it depends on your data if you want return only one data you should use first(). If you want to return a lot of data you should use get().
Controller:
$users = DB::table('accounts')
->join('users', 'accounts.user_id', '=', 'users.id')
->select('users.accno', 'accounts.*')
->get();
Blade:
#foreach($users as $user)
<tr>
<td>{{ $user->accno }}</td>
<td>{{ $user->balance }}</td>
<td>{{ $user->amt }}</td>
</tr>
#endforeach

Laravel - Get data from another table (no relationship) and display on view

I'm new to laravel and I can't seem to display a data from another table that is not related. I have three tables, Department, Section and Position.
Department Has Many Section
Section Has Many Position
I want to display the department name from the Department table in the position view.
Position Controller
$getPositionWithSection = PositionModel::with('section')->get();
return view('PositionView.add', compact('getPositionWithSection'));
Position View HTML
#foreach($getPositionWithSection as $id => $employee)
<tr>
<td>{{ ++$id }}</td>
<td>{{ $employee->section->department_name }}</td>
<td>{{ $employee->section->section_name }}</td>
<td>{{ $employee->position_name }}</td>
</tr>
#endforeach
I was able to display the name of the section, but how do I display the name of the department?
#foreach($getPositionWithSection as $id => $employee)
<tr>
<td>{{ ++$id }}</td>
<td>{{ $employee->section->department->department_name}}</td>
<td>{{ $employee->section->section_name }}</td>
<td>{{ $employee->position_name }}</td>
</tr>
#endforeach

How to display count result to blade template

I have attendance table then I perform count to total all the Present(P)Absent(A)Late(L) now how can I show it to blade template inside the html input with 3 separated value example: value=P, value=A, value=L;
right now with my code I'm getting it as all
controller:
public function get_status(){
$from = date('2018-01-01');
$to = date('2018-03-31');
$atnds = DB::table('attendances')
->select(DB::raw('count(*) as total, status'))
->where('status', '<>', 'status')
->whereBetween('days', [$from,$to])
->groupBy('status')
->where('lead_id', '=', 1)
->get();
return view('total',compact('atnds'));}
DD RESULT
[{"total":7,"status":"A"},{"total":9,"status":"L"},{"total":65,"status":"P"}]
If you want with input then you can use it like this
<table style="width:100%">
<tr><th>Status </th> <th>Total</th></tr>
#foreach($atnds as $at)
<tr>
<td>{{ $at->status }}</td>
<td><input type="text" value ="{{ $at->total }}" /></td>
</tr>
#endforeach
</table>
In blade you could use
<table style="width:100%">
#foreach($atnds as $ad)
<tr>
<td>{{ $ad->total }}</td>
<td>{{ $ad->status }}</td>
</tr>
#endforeach
</table>
If you want to break them out i would do so before returning it to the blade
$status = []
foreach($atnds as $ad) {
$status[$ad->status] => $ad->total
}
$status = collect($status);
return view('total',compact('status'));
and in the blade you should now be able to do
{{ $status->L }}
or
{{ $status['L'] }}

Laravel Eloquent where function doesn't work(?)

If got the following database table:
Now I want to display the rows where where "inventory_warning" is greater then the "inventory" field. Like the second results as show below:
In my first example I do a where statement using Eloquent and try to get the rows, but it doesn't yield correct results (it retrieves all rows). My second example shows the check done with a PHP-if statement, which works but will be very inefficient when the table contains more data..
Shortened code:
<?php $products = Product::where('inventory_warning', '>', 'inventory')->get();?>
<tr>
<th colspan="3">First results:</th>
</tr>
#foreach($products as $product)
<tr>
<td># {{ $product->id }} - {{$product->item->name}}</td>
<td>{{ $product->inventory }}</td>
<td>{{ $product->inventory_warning }}</td>
</tr>
#endforeach
<?php $products = Product::all();?>
<tr>
<th colspan="3">Second results:</th>
</tr>
#foreach($products as $product)
#if($product->inventory_warning > $product->inventory)
<tr>
<td># {{ $product->id }} - {{$product->item->name}}</td>
<td>{{ $product->inventory }}</td>
<td>{{ $product->inventory_warning }}</td>
</tr>
#endif
#endforeach
What is going on here? Is this a possible Eloquent bug or am I doing something wrong?
Comparing two database fields isn't supported by the where function as far as i know.
You'll have to use whereRaw
Product::whereRaw('inventory_warning > inventory')->get();

Categories