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();
Related
So, Basically the Company can have one, two or no director and I want to display based on the company profile.
I want to display table like below (please don't mind the red border, I just want to highlight what I really want to changed in my table):-
and this is what I got:
this is my loop for displaying inside the table:
<table class="table table-borderless table-sm table_view2">
<thead>
<tr class="spaceSmall">
<td style="width:50%;padding-left: 10px;">NOTES TO ACCOUNT<br>Schedules referred to above and notes attached there to form an integral part of Balance Sheet.<br>This is the Balance Sheet referred to in our Report of even date.</td>
<td style="width:25%"></td>
<td style="width:25%"></td>
</tr>
</thead>
<tbody>
<tr class="spaceSmall">
<td style="padding-left: 25px;">{{ $auditor->auditor_name }}<br>{{ $auditor->qualification }}<br>Membership No. : {{ $auditor->membership_number }}</td>
#foreach($tasks->work->company->directors as $director)
#if($director->pivot->director_type == 'Both' || $director->pivot->director_type == 'Director')
#if(count($director) == 1)
<td></td>
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#elseif(count($director) == 2)
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#else
<td></td>
<td></td>
#endif
#else
<td></td>
<td></td>
#endif
#endforeach
</tr>
</tbody>
</table>
If company doesn't have director or if company has 1 director, then it is working perfectly, but if company has 2 directors then it try to insert at the 3rd and 4th column but I don't have that 4th column, If I removed the if condition inside that foreach then it's displaying like the 1st image but I have to check the director(s) they had.
Thanks in Advance
I got the answer, for anyone seeking the answer I just put this line outside the loop then everything works fine.
#php $countDirector = count($tasks->work->company->directors); #endphp
And inside my foreach
#if($countDirector == 1)
<td></td>
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#elseif($countDirector == 2)
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#else
<td></td>
<td></td>
#endif
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
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?
I have a view in which I have to execute foreach loops. The problem is that I want to get one input from first foreach then get the other input from the second foreach. I have an outer for loop which will execute until all the foreach is exhausted.
#for($i=0;$i<=$n;$i++)
<tr>
<th scope="row">{{$i}}</th>
#foreach($Names as $Name)&&
<td>{{$Name}}</td>
#endforeach
#foreach($users as $user)
<td>{{$user}}</td>
#endforeach
#endfor
try like this
#foreach($Names as $key => $val)&&
<td> {{$val}}</td>
<td> {{$users[$key]}} </td>
#endforeach
Your making a mistake with your structure here. You should have the Users and there names and username in one variable so you can foreach this result.
#foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->user }}</td>
</tr>
#endforeach
If you want to add numbers to the list just add a counter with css or php if thats your way.
<?php $i=1; ?>
#foreach($users as $user)
<tr>
<td>{{ $i }}
<td>{{ $user->name }}</td>
<td>{{ $user->user }}</td>
</tr>
<?php $i++; ?>
#endforeach
thanks for trying guys. I got the answer...
#foreach($Names as $Name)
<tr>
<th scope="row">{{$i++}}</th>
{{--#foreach(#Names as $Name)--}}
<th> {{$Name}}</th>
#foreach($users as $user )
<td> {!! Form::text($users.$i,null, array('class' => 'form-control','id'=>$i)) !!} </td>
#endforeach
</tr>
#endforeach
The catch was that for loop and foreach required the same number of iterations. So we can simply eliminate for loop.
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.