I have 2 tables like this:
Product table
+----+-----------+------------+
| id | name | price |
+----+-----------+------------+
| 1 | Product 1 | 20 |
| 2 | Product 2 | 10 |
| 3 | Product 3 | 50 |
| 4 | Product 4 | 40 |
+----+-----------+------------+
prices table
+----+-------+------------+---------------------+
| id | price | product_id | created_at |
+----+-------+------------+---------------------+
| 1 | 20 | 1 | 2014-06-21 16:00:00 |
| 2 | 10 | 1 | 2014-06-21 17:00:00 |
| 3 | 50 | 2 | 2014-06-21 18:00:00 |
| 4 | 40 | 2 | 2014-06-21 19:00:00 |
+----+-------+------------+---------------------+
in my index.blade.php I have:
#foreach($products as $product)
#foreach($prices as $price)
<tr>
<td scope="row>">{{$product->id}}</td>
<td >{{$product->name}}</td>
<td>{{$price->price}}</td>
</tr>
#endforeach
#endforeach
but how do I get it that where the {{ $price->price }} is that i only get the latest price to be shown.
You could modify your code to this
In your Product.php create a relationship
public function prices() {
return $this->hasMany(Price::class);
}
#foreach($products as $product)
<tr>
<td scope="row>">{{$product->id}}</td>
<td >{{$product->name}}</td>
<td>{{$product->prices()->latest()->price}}</td>
</tr>
#endforeach
Related
NET MVC, I have one table, I want to calculate amount of similar Id's in row total.
| Id | Name | amount |
| -- | ---- | ------ |
| 1 | abc | 1000 |
| 1 | xyz | 2000 |
| 2 | mno | 1000 |
| 3 | ams | 500 |
| 3 | dky | 400 |
I print this in MVC View using for each loop
foreach(var item in Model){
<tr>#item.id</tr>
<tr>#item.Name</tr>
<tr>#item.amount</tr>
}
but I want to display table like :-
| Id | Name | amount |
| -- | ---- | ------ |
| 1 | abc | 1000 |
| 1 | xyz | 2000 |
|total 3000 |
| 2 | mno | 1000 |
|total 1000 |
| 3 | ams | 500 |
| 3 | dky | 400 |
|total 900 |
what should I do for adding that total row after similar ID,s and calculate total amount of that ID?
Thanks in advance.
This is one way to do it:
#{
ViewData["Title"] = "Home Page";
var groups = Model.GroupBy(x => x.Id).Select(g => new
{
Id = g.Key,
Items = g.ToList(),
Total = g.Sum(x => x.Amount)
});
}
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach (var group in groups)
{
foreach(var item in group.Items)
{
<tr>
<td>#item.Id</td>
<td>#item.Name</td>
<td>#item.Amount</td>
</tr>
}
<tr>
<td colspan="2">Total</td>
<td>#group.Total</td>
</tr>
}
</tbody>
</table>
This is my product table.I want to store customer_id from 1000 and save by +1 how much data i stored
id | customer_id | name |
1 | 1000 | ABC |
2 | 1001 | Tripathi |
3 | 1002 | Leaptrig |
4 | 1003 | Falcon |
5 | 1004 | Savillan |
6 | 1005 | Molt |
7 | 1006 | Falt |
My Controller
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=1000+($lastProduct+1);
}
$product->save();
But In this code,Customer id i increment by 1000 2001,3002 like this. so how should i avoid it ?
id | customer_id | name |
1 | 1000 | ABC |
2 | 2001 | Tripathi |
3 | 3002 | Leaptrig |
4 | 4003 | Falcon |
5 | 5004 | Savillan |
6 | 6005 | Molt |
7 | 7006 | Falt |
You can try this :-
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=$lastProduct+1;
}
$product->save();
I have some question about my code, i had table like these :
tb teacher
|---------------------------------|
|id | name | id_prodi|
-----------------------------------
|1 | Teacer A | 1 |
|2 | Teacher B | 2 |
|3 | Teacher C | 1 |
tb prodi
|------------------------
|id | prodi |
-------------------------
|1 | Prodi 1 |
|2 | Prodi 2 |
|3 | Prodi 3 |
tb year
|------------------------
|id | year |
-------------------------
|1 | 2018 |
|2 | 2019 |
|3 | 2020 |
tb attendence
|---------------------------------------------------|
|id | teacher_id | year_id | date_in |
-----------------------------------------------------
| 1 | 1 | 3 | 2020-03-20|
| 2 | 1 | 3 | 2020-03-21|
| 3 | 3 | 3 | 2020-03-21|
| 4 | 1 | 3 | 2020-03-22|
thats my tables. what I need to do here, get result like this on my datatable
i have some condition with combobox to select by a year
|----------------------------------------------------------|
|# | name | year | total_attend |
------------------------------------------------------------
| 1 | Teacher A | 2020 | 3 |
| 2 | Teacher C | 2020 | 1 |
some like thats for final result
and this my controller
public function showData(Request $request)
{
$year = $request->year_id;
$prodi_id = $request->prodi_id;
$datas = Teacher::whereHas('attendance', function($query) use ($year) {
$query->where('year_id', $year);
})
->where('prodi_id',$prodi_id)
->get();
return response()->json([
'success' => 'Success',
'data' => $datas
]);
}
my problem is, I cant get total column by count the attendance year. Anyone has clue??
--help
You can achieve this using selectRaw() with a count() function for teacher_name:
DB::table('attendence')->selectRaw('t.name as name,y.year as year,count(t.name) as total_attend')
->join('teacher as t','t.id','=','attendence.teacher_id')
->join('year as y','attendence.year_id','=','y.id')
->where([['attendence.year_id',$year],['t.prodi_id',$prodi_id]])
->groupBy('name','year')->get();
I am trying to get the count of unique batches in gift_code table for each campaign. The gift_code table is joined to campaign table by campaign_id.
Here is some sample data for campaign table.
--------------+--------------
|campaign_id | name |
--------------+--------------
| 1 | abc |
--------------+--------------
| 2 | xyz |
--------------+--------------
Below is some sample data for gift_code table.
--------------+------------------------+--------------+
|gift_code_id | campaign_id | batch | unique_code |
--------------+-------------+----------+---------------
| 1 | 1 | 1 | zxc23 |
--------------+-------------+----------+--------------+
| 2 | 1 | 2 | rtc26 |
--------------+-------------+----------++-------------+
| 3 | 2 | 1 | z8723 |
--------------+-------------+----------+--------------+
| 4 | 2 | 2 | h7c26 |
--------------+-------------+----------++-------------+
| 5 | 2 | 2 | rrcf6 |
--------------+-------------+----------++-------------+
| 6 | 2 | 3 | r7y28 |
--------------+-------------+----------++-------------+
| 7 | 2 | 3 | bnc26 |
--------------+-------------+----------++-------------+
$campaign = DB::table('campaign')
->select('campaign.*', DB::raw('count(gift_code.batch) as batch_count')->groupBy('gift_code.campaign_id')->groupBy('gift_code.batch'))
->leftjoin('gift_code', 'campaign.campaign_id', '=', 'gift_code.campaign_id')
->get();
My expected results are:
--------------+-------------------------+
|campaign_id | name |batch_count|
--------------+-------------+-----------+
| 1 | abc | 2 |
--------------+-------------+-----------+
| 2 | xyz | 3 |
--------------+-------------+-----------+
Try below query
$data = \DB::table('campaign as c')
->leftJoin('gift_code as gc','c.campaign_id','=','gc.campaign_id')
->select('c.*',\DB::raw('COUNT(distinct(gc.batch)) as batch_count'))
->groupBy('c.campaign_id')
->get();
I am creating a duty roster system. I want to display each duty roster date in one row. I am able to it, but it seems like very manual way. I am looking any more effective way to accomplish it.
Currently, in the view file, each duty, I have run one foreach and if else to check the duty role. If there is no duty on the particular, it will have repeat '-' show in the column.
Below is my table structure.
I have my standard Laravel Users table.
duties:-
+----+----------------+-----------------+--------+---------------------+---------------------+
| id | key | name | status | created_at | updated_at |
+----+----------------+-----------------+--------+---------------------+---------------------+
| 1 | perfoman | Perfoman | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 2 | chair_person | Chair Person | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 3 | song_leader | Song Leader | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 4 | backup_singer1 | Backup Singer 1 | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 5 | guitarist | Guitarist | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 6 | usher1 | Usher 1 | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 7 | usher2 | Usher 2 | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 8 | pa | PA SYS | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
| 9 | projector | Projector | Active | 2017-12-12 15:56:26 | 2017-12-12 15:56:26 |
+----+----------------+-----------------+--------+---------------------+---------------------+
This is my duty_rosters table:-
+----+------------+---------+---------+---------------------+---------------------+
| id | duty_date | duty_id | user_id | created_at | updated_at |
+----+------------+---------+---------+---------------------+---------------------+
| 13 | 2017-12-16 | 1 | 1 | 2017-12-12 16:52:48 | 2017-12-12 16:52:48 |
| 14 | 2017-12-16 | 2 | 1 | 2017-12-12 16:52:48 | 2017-12-12 16:52:48 |
| 15 | 2017-12-16 | 3 | 1 | 2017-12-12 16:52:48 | 2017-12-12 16:52:48 |
| 16 | 2017-12-23 | 1 | 2 | 2017-12-12 16:57:15 | 2017-12-12 16:57:15 |
| 17 | 2017-12-23 | 2 | 2 | 2017-12-12 16:57:15 | 2017-12-12 16:57:15 |
| 18 | 2017-12-23 | 3 | 2 | 2017-12-12 16:57:15 | 2017-12-12 16:57:15 |
| 19 | 2017-12-23 | 4 | 2 | 2017-12-12 16:57:15 | 2017-12-12 16:57:15 |
+----+------------+---------+---------+---------------------+---------------------+
Inside Controller:-
$duty_rosters = DutyRoster::with(['user', 'duty'])->get();
$duty_rosters = $duty_rosters->groupBy('duty_date');
This is my collection output:-
Collection {#647 ▼
#items: array:2 [▼
"2017-12-16" => Collection {#612 ▼
#items: array:3 [▼
0 => DutyRoster {#576 ▶}
1 => DutyRoster {#577 ▶}
2 => DutyRoster {#578 ▶}
]
}
"2017-12-23" => Collection {#535 ▶}
]
}
In my view file, I have a table to show each column for the duty role.
<table class="table table-hover">
<tbody><tr>
<th>Date</th>
<th>Perfoman</th>
<th>Chair Person</th>
<th>Song Leader</th>
<th>Backup Singer</th>
<th>Guitarist</th>
<th>Usher</th>
<th>PA SYS</th>
<th>Projector</th>
</tr>
#forelse($duty_rosters as $date => $duty_roster)
<tr>
<td>{{ $date }}</td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'perfoman'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'chair_person'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'song_leader'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'backup_singer1'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'usher1'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'usher2'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'pa'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
<td>#foreach($duty_roster as $duty) #if($duty->duty->key == 'projector'){{ $duty->user->name }}#else {{ '-' }} #endif #endforeach </td>
</tr>
#empty
<tr>
<td colspan="100">No Data</td>
</tr>
#endforelse
</tbody>
</table>
When I see my view file, I know something is not correct... it look so ineffective. Any idea how to improve it?
Share my solution here:
Maybe not the best solution, but at least cleaner.
I create a helper function:-
if (!function_exists('getDutyPersonName')) {
/**
* Blade Helper getDutyPersonName
*
* #param $duty
* #return boolean
*/
function getDutyPersonName($duty_roster, $duty)
{
$duty_name = $duty_roster->first(function ($value, $key) use ($duty) {
return $value->duty->key == $duty;
});
if(!is_null($duty_name))
return ($duty_name->user->name);
return '-';
}
}
In the blade view file:-
<td>{{ getDutyPersonName($duty_roster, 'chair_person') }}