I have a prices table that stores the date which a product cost was stored. There is a relationship between the product & price model which works very well by returning the collection. I want to display a table with a date & product name as the columns, then display the DISTINCT dates & the corresponding product cost on each row. Below is my blade view code and what the table looks like
view - code
<table class="table table-striped">
<thead>
<th>Date</th>
#if (isset($products) && !empty($products))
#foreach ($products as $product)
<th>{{ ucwords($product->name) }}</th>
</thead>
<tbody>
#foreach ($product->prices as $price)
<tr>
<td>{{ $price->date }}</td>
<td>{{ $price->cost }}</td>
</tr>
#endforeach
#endforeach
#else
Product price is not available
#endif
</tbody>
</table>
browser-view
Changing my code by ending the first foreach loop, gives me something close to what i want but only the cost for the last product is displayed
alternative code
<table class="table table-striped">
<thead>
<th>Date</th>
#if (isset($products) && !empty($products))
#foreach ($products as $product)
<th>{{ ucwords($product->name) }}</th>
#endforeach
</thead>
<tbody>
#foreach ($product->prices as $price)
<tr>
<td>{{ $price->date }}</td>
<td>{{ $price->cost }}</td>
</tr>
#endforeach
#else
Product price is not available
#endif
</tbody>
</table>
Browser-view
How do i display all of my product cost values for each date in the format of the last picture?
Related
I just trying to make a group of students who have the same group ID and calculate the CGPA of only those students who have belong to the same Group.
Refrence Example
Here's is the code for Controller
public function View_GroupStudent()
{
$allot_app = AllotmentApps::select(
"AllotmentApps.grpID",
"AllotmentApps.sname",
"AllotmentApps.cgpa"
)
->orderBy('grpID')
->get();
return view('deny', compact('allot_app'));
}
Views:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
<tr>
#foreach ($allot_app as $allot_app)
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ $allot_app->sum('cgpa') }}</td>
</tr>
#endforeach
</table>
Also help me in creating a proper view for that.
I was confused by your image because the red numbers weren't the averages at all. You should be able to achieve what you're trying to do with the following:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
#foreach ($allot_app->groupBy('grpID') as $grouped)
#foreach ($grouped as $allot_app)
<tr>
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ number_format($grouped->avg('cgpa'), 2) }}</td>
</tr>
#endforeach
#endforeach
</table>
i need help with my foreach loop in view
Controller.php
$expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id');
$categories = Category::all();
return view('dashboard.index', compact('expenses','categories'));
index.php
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
<td>{{ $expense }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I get the output like this.
enter image description here
It outputs the expense 2 times, is there a limiter for "foreach loop" or is there another way on doing this?
You need to add a condition for that $category->id is the same as category_id
#foreach($expenses as $key => $expense)
#if($key == $category->id)
<td>{{ $expense }}</td>
#endif
#endforeach
The right way to do it is by using join or relationship.
$expenses = Expense::join('categories','categories.category_id','expenses.category_id')->groupBy('expenses.category_id')->selectRaw('sum(expenses.amount) as sum, expenses.category_id','categories.name')->get();
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($expenses as $expense)
<th>{{ $expense->name }}</th>
<td>{{ $expense->sum }}</td>
#endforeach
</tbody>
</table>
You should limit the results in controller, but here's how you can accomplish this in a blade. Not pretty
#foreach ($element['subs'] as $item)
#if($loop->index < 10)
// Your code
#endif
#endforeach
You should compare category_id with category->id.
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
#if($expense->category_id == $category->id )
<td>{{ $expense->sum }}</td>
#endif
#endforeach
</tr>
#endforeach
I'm sorry, I have a problem with my laravel pagination Gives me one by one page for example, in my table 16 row I make paginate (10), the pagination gives me in the first page from 1 to 10 and in the second page from 1 to 6 I want the normal pagination from 1 to 10 and from 11 to 16 any help, please
public function allappointmnts(){
$allapo=DB::table('bookappoitments')->orderBy('times.id')
->join('users','bookappoitments.users_id','users.id')
->join('times','bookappoitments.times_id','times.id')
->join('dates','bookappoitments.Dates_id','dates.id')
->paginate(10);
return view('admin.Managers.allappoinments',compact('allapo'));
}
in the blade page:
<div class="text-center">
{!! $allapo->links(); !!}
</div>
The table :
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Time</th>
<th>Employee</th>
<th>Name</th>
<th>Email</th>
<th>Company</th>
<th>Phone</th>
<th>Location</th>
<th>Remarks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($allapo as $Appointments)
<td>{{ $loop->index+1 }}</td>
<td>{{ $Appointments->Dates }}</td>
<td>{{ $Appointments->from_to }}</td>
<td>{{ $Appointments->name }}</td>
<td>{{ $Appointments->gustname }}</td>
<td>{{ $Appointments->gustemail }}</td>
<td>{{ $Appointments->gustcompany }}</td>
<td>{{ $Appointments->gustphone }}</td>
<td>{{ $Appointments->Location }}</td>
<td>{{ $Appointments->Remarks }}</td>
<td>
<a class="btn btn-success btn-mini deleteRecord " href="{{url('showbymanagment',$Appointments->id)}}">Show</a>
</td>
</tr>
#endforeach
Your problem is that you're using the current index of each loop - which will always be within the range of 1-10 of each page (since you have 10 elements per page).
You need to get the current page you're on using
$allapo->currentPage()
Then get the number of elements per page you got, using
$allapo->perPage()
Multiply these two, and it will be the base number for your pagination, meaning that its this number you should increment from.
The indexed table should then be
{{ $allapo->currentPage() * $allapo->perPage() + $loop->index }}
instead of
{{ $loop->index+1 }}
Then, to fix the heading-numbers (10 and 6 at the top), get the total number of results instead of the count of the page using
$allapo->total()
See the Laravel documentation for more details.
Not entirely sure, but you will need to write some sort of function to fix this.
Essentially you need to do this:
Get the page number (ex: 1)
Multiply it by the rows per page (ex: 10)
Add the iteration number
Subtract the per page number
function correct_pagination_numbers($cp, $pp, $counter)
{
$c = (($pp * $cp) + $counter) - $pp;
return $c;
}
Then you can call the function in your view like so:
<td>{{ correct_pagination_numbers($allapo->currentPage(), $allapo->perPage(), $loop->index) }}</td>
It would probably be the best solution to do this in a helper file. If you don't know how to create a helper file and autoload it, you can look here.
Try this code:
<?php $i = $lists->perPage() * ($lists->currentPage() - 1); ?>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Time</th>
<th>Employee</th>
<th>Name</th>
<th>Email</th>
<th>Company</th>
<th>Phone</th>
<th>Location</th>
<th>Remarks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($allapo as $Appointments)
<td><?php $i++; ?>{{ $i }} </td>
<td>{{ $Appointments->Dates }}</td>
<td>{{ $Appointments->from_to }}</td>
<td>{{ $Appointments->name }}</td>
<td>{{ $Appointments->gustname }}</td>
<td>{{ $Appointments->gustemail }}</td>
<td>{{ $Appointments->gustcompany }}</td>
<td>{{ $Appointments->gustphone }}</td>
<td>{{ $Appointments->Location }}</td>
<td>{{ $Appointments->Remarks }}</td>
<td><a class="btn btn-success btn-mini deleteRecord " href="
{{url('showbymanagment',$Appointments->id)}}">Show</a></td>
</tr>
#endforeach
Try ths
#foreach($allapo as $key => $Appointments)
{{ $key + $allapo->firstItem() }}
#endforeach
to see the correct number change the line
<td>{{ $loop->index+1 }}</td>
to:
<td>{{ ($allapo->currentPage() - 1) * $allapo->perPage() + $loop->iteration }} <td>
new to Laravel, and just having this problem that I couldn't figure it out.
I have a post controller set up to query out all blog posts. On the database, I have a integer column and named as "status", 0 as inactive, 1 as active. The question is how and where to place the logic to check whetherthe status is 0 or 1, and show "inactive" or "active" in the view. Thanks in advance.
public function index()
{
$posts = Post::where('user_id', 1)->orderBy('id', 'desc')->paginate(5);
return view('post.index')->withPosts($posts);
}
Here is the view
<table class="table">
<tr>
<th>Date Created</th>
<th>Title</th>
<th>Status</th>
</tr>
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td></td>
</tr>
<br>
#endforeach
</table>
you can have that logic inside your blade template:
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td>#if($post->status) active #else inactive #endif</td>
</tr>
<br>
#endforeach
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();