How to join two tables in Laravel - php

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

Related

Laravel Join and groupBy Cannot Show Column Data

$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?

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 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'] }}

Check if no records exist in view laravel

Okay so I've hit a wall for some reason and I can't figure this out. I wan't to check if there are any records in a query and then use an #if in my view to say either "yes, there are records" or "none". Nothing I've tried is working..
What I want:
<div class="col-md-12 col-style">
<h1 class="no-border">YOUR LATEST CONVERSIONS</h1>
#if(something here that lets me test if $transactions is empty)
<table class="table table-striped">
<tr>
<th>#</th>
<th>Type</th>
<th>User</th>
<th>Amount</th>
<th>Value</th>
<th>Result</th>
<th>Result Value</th>
<th>Date</th>
</tr>
#foreach($transactions as $transaction)
<tr>
<td>{{ $transaction->id }}</td>
<td>{{ $transaction->status }}</td>
<td>{{ $transaction->username }}</td>
<td>{{ $transaction->amount }}</td>
<td>{{ $transaction->value }}</td>
<td>{{ number_format($transaction->result, 2) }}</td>
<td>{{ $transaction->result_value }}</td>
<td>{{ $transaction->created_at }}</td>
</tr>
#endforeach
</table>
#else
yo no records
#endif
</div>
My controller:
public function index($username)
{
$user = User::where('username', $username)->firstOrFail();
$id = $user->id;
$transactions = Transactions::where('user_id', '=', $id)->take(5)->get();
return view('user.profile', compact('user', '=', 'userCurrency', 'transactions'));
So if you see in the top part there's a basic #if. Maybe I'm over thinking it, or am I passing in something that cant be checked the way I want it to?
Thanks!
#if (!$transactions->isEmpty())
...
#endif
this use laravel collection method do the trick.
it is as easy as:
#if(count($transactions) > 0)
#if($transactions) should do the trick.

How to do paginate in Laravel?

so what i want is to use paginate in my view but i got always an error Call to a member function paginate() on a non-object, this is my code :
First my Action :
public function index()
{
$logs = DB::table('services')
->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})
->get()->paginate(10); //here i got the error
return View::make('logs.index',array('logs'=> $logs,'title'=>'Service Logs'));
}
Then my view :
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Domain</td>
<td>Port</td>
<td>Checktime</td>
<td>Status</td>
<td>Responce</td>
</tr>
</thead>
<div class="container">
#foreach($logs as $key => $value)
<tr>
<td>{{ $value->domain }}</td>
<td>{{ $value->service_port }}</td>
<td>{{ $value->checktime }}</td>
<td class="text-center">
#if( $value->status == 'up' ) <img src="../img/up3.png" />
#elseif( $value->status == 'down' ) <img src="../img/down3.png" />
#else <img width="30" height="30" src="../img/warning_icon.png" />
#endif
</td>
<td>{{ $value->response_time }}</td>
</tr>
#endforeach
</div>
</table>
{{$logs->links();}}
So please if someone has any idea i will be so appreciative
You do not need to use get when you are using pagination. Your query should simply be:
$logs = DB::table('services')->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})->paginate(10);
In fact you can remove that closure as well since this really isn't a complex join.
$logs = DB::table('services')
->join('logs', 'services.id', '=', 'logs.service_id')
->paginate(10);
From the docs (http://laravel.com/docs/pagination) it seems that the correct usage is:
$logs = DB::table('services')
->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})
->paginate(10); //removed get()

Categories