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');
Related
$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?
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
#foreach($products as $product)
<tr class="product">
<td>{{ $product->product_name }}</td>
<td>${{ $product->price }}</td>
<td>{{ $product->category }}</td>
<td>{{ $product->product_description }}</td>
<td>Delete</button></td>
#endforeach
Hi, how do program this code such that if the catagory_id = 1 "Sushi" is displayed and if catagory_id = 2 "Drinks" is displayed.
As of now this is the display:
enter image description here
Database
You should write like this
In Product Model
public function category()
{
return $this->belongTo(Category::class);
}
In Ctonroller
$products = Product::with('category')->paginate()
In Blade
#foreach($products as $product)
<tr class="product">
<td>{{ $product->product_name }}</td>
<td>${{ $product->price }}</td>
<td>{{ $product->category->name }}</td>
<td>{{ $product->product_description }}</td>
<td>Delete</button></td>
#endforeach
In your problem first you need to fetch all the associate category..
You can do it with eager loading .. https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
Then you need to check the category condition..
$products = App\PRODUCTMODELNAME::with('category')->get();
#foreach($products as $product)
<tr class="product">
<td>{{ $product->product_name }}</td>
<td>${{ $product->price }}</td>
<td>
#if($product->category->id === 1)
Sushi
#elseif($product->category->id === 1)
Drinks
</td>
<td>{{ $product->product_description }}</td>
<td>Delete</button></td>
#endforeach
I want a help to change this function of a model in my project cause it returns an array like this :: [{"Package_Name":"Excel"}] i need only the string "EXCEL"
Code in model
public function getUserSeminars(){
$seminar = Seminar::select('Package_Name')
->join('Home_StudentPackages','Home_StudentPackages.hsp_PackID','=','Home_Packages.Package_ID')
->join('Home_Students','Home_Students.home_id','=','Home_StudentPackages.hsp_homeStudID')
->where('Home_Students.home_id','=',$this->home_id)
->get();
return $seminar;
}
blade
#foreach($users as $i=>$user)
<tr>
<td>{{ $i+1 }}</td>
<td>{{ $user->home_lastname }}</td>
<td>{{ $user->home_firstname }} </td>
<td>{{ $user->getUserSeminars() }}</td>
<td>{{ $user->home_id }}</td>
<td>{{ $user->getUserRights() }}</td>
#foreach($users as $i=>$user)
<tr>
<td>{{ $i+1 }}</td>
<td>{{ $user->home_lastname }}</td>
<td>{{ $user->home_firstname }} </td>
<td>
<ul>
#foreach($user->getUserSeminars() as $seminar)
<li>{{$seminar->Package_Name}}</li>
#endforeach
</ul>
</td>
<td>{{ $user->getUserSeminars() }}</td>
<td>{{ $user->home_id }}</td>
<td>{{ $user->getUserRights() }}</td>
Kind of some pseudo code, can't remember 100% correct sintaxis, but this should work.
Controller:
$users = DB::table('users')
->join('uploads', 'users.id', '=', 'uploads.user_id')
->select(DB::raw('users.*, COUNT(*) AS total_uploads'))
->orderby('total_uploads', 'desc')
->groupby('users.id')
->take(5)
->get();
View:
<?php $i = 1 ?>
#foreach($users as $user)
<tr>
<td>{{ $i }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->total_uploads }}</td>
</tr>
<?php $i++ ?>
#endforeach
Expected output:
1 John 99
2 Mary 78
3 Jill 57
3 Bill 57
5 Hans 56
How can I modify this to account for ties?
Try this:
<?php $i = 0;
$last_count = null;
?>
#foreach($users as $user)
<?php if($user->total_uploads != $last_count) $i++; ?>
<tr>
<td>{{ $i }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->total_uploads }}</td>
</tr>
<?php $last_count = $user->total_uploads; ?>
#endforeach
The counter will only increment if the current user's total_uploads is different from the last user's total_uploads.