Check if no records exist in view laravel - php

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.

Related

Laravel view foreach with two properties

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

Laravel pagination problem one by one pages

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>

Foreach loop (laravel blade)

I need help with a foreach loop in laravel. For some reason I need to have two rows instead of two tables (would have solved my problem) However, I have problems with it getting to work due to not knowing where to place my tags to get the right table structure.
Code:
<table class="minimalistBlack">
<tbody>
<tr>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
<th>X</th>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
</tr>
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
#foreach ($data2 as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
#endforeach
</tbody>
</table>
How can I combine the two foreach loops and and get the right table structure?
to achieve just that result you could do
foreach (array_combine($courses, $sections) as $course => $section)
but that only works for two arrays
or u can use two table and make feel as single table
<div class="row">
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
Assuming the $data and $data2 variables are Laravel collection instances, you can simply use $data = $data->concat($data2). Now $data will contain data of both variables. For a simple array, you can use $data += $data2;. This will merge $data & $data2 to $data. Then you can simply use:
...
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach
</tbody>
</table>
User array_merge function in controller like this
$array1 = array("john","ny");
$array2 = array("mitchel","ny");
$result = array_merge($a1,$a2);
if $data and $data2 have one, one row you can try like this.
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
</tr>
#endforeach
#foreach ($data2 as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach

How to us timestamp in query builder in laravel 5.3 to tack users registered in last 5 days

I want to list down the users who are registered in last 5 day.. To list down all the users 1 use following query..
$users = User::select('*')->orderby('created_at', 'desc')->get();
and fetch it in view like this.
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td>{{ \App\Points::getUserPoints($u->id) }}</td>
</tr>
<?php }?>
</tbody></table>
but what will be the query to fetch users who have registered in last 5 days..?? any one please tell me a good query..
You can use Carbon (docs) to do this:
$now = Carbon::now();
$users = User::where('created_at', '>=', $now->subDays(5))->get();
Make sure to "use Carbon\Carbon;" at the top of the file.

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Categories