I'm working with Laravel 8 to develop my project, and in this project, I have a Controller named HomeController that his this method:
public function index()
{
$questions = Question::latest()->limit(5)->get();
return view('home', compact('questions'));
}
And then I get results like this on blade:
#php
if ($questions)
{
#endphp
<table class="table table-striped table-bordered table-responsive BKoodakBold">
<thead class="thead-light">
<tr>
<th scope="col" class="forum-col" style="text-align:right;">Question</th>
<th scope="col" style="text-align:right;">Answer</th>
<th scope="col" style="text-align:right;">Rate</th>
<th scope="col" style="text-align:right;">Views</th>
</tr>
</thead>
<tbody>
#foreach($questions as $question)
<tr>
<td style="text-align:right;">
<h3 class="h5 mb-0">{{ $question->title }}</h3>
</td>
</tr>
#endforeach
</tbody>
</table>
#php
}
#endphp
As you can see I set if ($questions) to check if this variable is not empty, do the for each loop next. And if it is empty, it should not show the table thead tags.
But now the problem is it does not work out... I mean even if I have no data at the DB, it still shows table thead completely.
So what is wrong with this code? How can I properly check if the table is empty or not?
I really appreciate it if you share your idea about this... Thanks.
You're returning collection, it's always true compact('questions')
Use
if (!$questions->isEmpty()) # Laravel(in-built) way
if (!$questions->isEmpty())
// show data
else
echo "No record found.";
check with this condition
#if (isset($questions) && count($questions) > 0)
Inside Code
#endif
Check additional condition as
&& count($questions) > 0
You retrieve an elocuent collection always, even if no data received. But collection is countable, so you can check it by
if(count($collection))
if count return zero then the sentence is assume like false
Related
I am beginner in both laravel and php. So, I have two variables and I want to iterate both of them into one table. I want <p class="text-warning">Izin sudah mau habis!</p> to be displayed inside <td>tenggat_izin</td> column. I tried this but it keeps looping all of them inside the table while i only want one of them which has the value of <31 for each row to display the warning text.
<script src="js/tower_kecamatan.js"></script>
<link rel="stylesheet" href="css/tower_kecamatan.css">
<h2 class="text-center">{{$region->kecamatan}}</h2>
<table class="table table-bordered col-md-12" id="search">
<thead class="thead-dark" >
<th scope="col">TOWER</th>
<th scope="col">ALAMAT</th>
<th scope="col">PERUSAHAAN</th>
<th scope="col">KOORDINAT</th>
<th scope="col">KETINGGIAN</th>
<th scope="col">PEMILIK TANAH</th>
<th scope="col">IZIN TOWER</th>
<th scope="col">TENGGAT IZIN</th>
<th scope="col">SHELTER DAN GENSET</th>
<th scope="col">LISTRIK</th>
<th scope="col">PAGAR TOWER</th>
<th scope="col">PAPAN NAMA TOWER</th>
<th scope="col">PETUGAS</th>
<th scope="col">HP PETUGAS</th>
</thead>
#foreach ($data as $x)
<tr>
<td>{{$x->tower_id}}</td>
<td>{{$x->desa}}</td>
<td>{{$x->pemilik_tower}}</td>
<td>{{$x->koordinat}}<p>lihat gambar</p></td>
<td>{{$x->ketinggian_meter}}</td>
<td>{{$x->pemilik_tanah}}</td>
<td>{{$x->izin_tower}}</td>
<td>{{$x->tenggat_izin}}
#foreach ($diff as $y)
#if ($y < 31)
<p class="text-warning">Izin sudah mau habis!</p>
#endif
#endforeach
</td>
<td>{{$x->shelter_genset}}</td>
<td>{{$x->listrik}}</td>
<td>{{$x->pagar_tower}}</td>
<td>{{$x->papan_nama}}</td>
<td>{{$x->petugas}}</td>
<td>{{$x->hp_petugas}}</td>
</tr>
#endforeach
Is there any way around this?
Looks good to me. I would add an #break to stop checking $diff values.
#foreach ($diff as $y)
#if ($y < 31)
<p class="text-warning">Izin sudah mau habis!</p>
#endif
#break;
#endforeach
I have a api with a unique number, the number is different, so when i call api with this unique number, i get back data, i am showing data in table. the table has a column name "amount".
as its dynamic data coming from an api, i am just stucked how i can show total amount in the very bottom of the table. i am using laravel.
my code sample
<table class="table table-one">
<thead class="table-success">
<tr>
<th scope="col">Inst. No.</th>
<th scope="col">PR No.</th>
<th scope="col">PR Date</th>
<th scope="col">Prem. Amt.</th>
</tr>
</thead><!-- ends: thead -->
<tbody>
#foreach ($results['polinfo'] as $data)
#foreach ($data['poldata'] as $res)
<tr>
<th scope="row">{{$res['INSTALNO']}}</th>
<td>{{$res['PR_NO']}}</td>
<td>{{$res['PR_DATE']}}</td>
<td>{{$res['AMOUNT']}}</td>
</tr>
#endforeach
#endforeach
</tbody><!-- ends: tbody -->
</table>
i have to calcualte all {{$res['AMOUNT']}} and have to show it as total amount.
Thanks in advance.
If you need the value only after the foreach you can get away with a separate variable to which to add the amounts:
#php($amount = 0)
#foreach ($results['polinfo'] as $data)
#foreach ($data['poldata'] as $res)
<tr>
<th scope="row">{{$res['INSTALNO']}}</th>
<td>{{$res['PR_NO']}}</td>
<td>{{$res['PR_DATE']}}</td>
<td>{{$res['AMOUNT']}}</td>
</tr>
#php($amount += (int) $res['AMOUNT'])
#endforeach
#endforeach
Amount: {{ $amount }}
I tried to sort/order data on database. I used this command:
SELECT * FROM `estates`
ORDER BY `estates`.`price` ASC
It take effect on database order. But on webpage it doesn't. How can I make it take effect on webpage too? Any idea? Thank you.
By the way I am using Laravel,
and retriving data from database with MVC
If you need to check to my page structure here it is:
<table cellspacing='0'> <!-- cellspacing='0' is important, must stay -->
<thead>
<tr>
<th width="150px">会社名</th>
<th width="150px">物件名</th>
<th width="250px">住所</th>
<th width="150px">販売価格</th>
<th width="100px">総戸数</th>
<th width="150px">専有面積</th>
<th width="100px">間取り</th>
<th width="100px">バルコニー面積</th>
<th width="100px">竣工時期</th>
<th width="100px">入居時期</th>
</tr>
<thead>
<tbody>
#foreach($estates as $estate)
<tr class="even">
<td>{{$estate->company_name}}</td>
<td>{{$estate->name}}<br/></td>
<td>{{$estate->address}}</td>
<td>{{$estate->price}}</td>
<td>{{$estate->hows_old}}</td>
<td>{{$estate->extend}}</td>
<td>{{$estate->rooms}}</td>
<td>{{$estate->balcon_m2}}</td>
<td>{{$estate->old}}</td>
<td>{{$estate->entery}}</td>
</tr>
#endforeach
</tbody>
</table>
And here is the controller:
public function sumos()
{
$estates = Estates::get();
//test
$data['estates'] = $estates;
return view('welcome', $data);
}
try to use orderBy
Estates::orderBy('price')->get();
View Code:
<li class="active"><a href="#tab_1" data-toggle="tab" id="versiontab"
onclick="window.location='{{ url("versiondb") }}'" >Version
Database</a></li>
This is my view code.This is also a tab .Onclicking the tab the url redirection works.
Route Code:
Route::get('versiondb','versiondbController#select');
Controllercode:
public function select()
{
$users=debModel::all();
return redirect()->back()->with($users);
}
Here it should fetches the values from db and redirecting back to the same page and display the retreived values in table .But i am getting an error here as $users undefined
Inside the Same view (specified above):
<table class="table" id="table">
#foreach($users as $users)
<thead>
<tr class="header">
<th valign="middle" width="3%">Versions</th>
<th valign="middle" width="2%">Supported versions</th>
<th valign="middle" width="10%">Release Date</th>
<th valign="middle" width="3%">Type</th>
<th valign="middle" width="10%">Description</th>
<th valign="middle" width="3%">Actions</th>
<th valign="middle" width="3%">Downloader</th>
<!--<th valign="middle" width="3%">Beta code</th>-->
</tr>
</thead>
<tbody>
<tr>
<td>{{$users->versions}}</td>
<td></td>
</tr>
#endforeach
</tbody>
</table>
I think this is the place where error occurs.
When you use "->with" in Laravel, Laravel go back to that page with a session message named with.
Usiing with in your scenario would be
public function select()
{
$users=debModel::all();
return redirect()->back()->with("message","I generated all users");
}
which would be accessed in your blade as
#if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
The solution to your question is write it this way
public function select()
{
$data['users']=debModel::all();
return view("viewdeb",$data);
}
then your blade you will need a foreach loop
#foreach ($users as $user)
{{$user->firstname}}
#endforeach
mind you, more data can be sent back to the view by just including them in the data array
public function select()
{
$data['users']=debModel::all();
$data['business']=businessModel::all();
return view("viewdeb",$data);
}
The issue is here:
#foreach($users as $users)
here your array variable name and the variable in which you are holding the data on each iteration is the same, so change it to:
#foreach($users as $user)
<td>{{$user->versions}}</td>
Use ($users as $user) instead ($users as $users) here
#foreach($users as $user)
And in your template
<td>{{$user->versions}}</td>
Also change in your controller
return view('haghwayUpdate')->with('users',$users);
Well that is my problem.
This is the public function in my model
public function traerDesc(){
return $this->hasManyThrough('App\modeloDescripcionVehiculos', 'App\modeloVehiculos', 'idDescripcion', 'id', 'idVehiculo');
}
This is the controller call
$data = [
'venta' => modeloAccion::where('accion', 'venta')->with('traerVehiculo', 'traerUsuario', 'traerCliente', 'traerTransaccion', 'traerVenta', 'traerDesc')->get(),
];
return view('modulos.general.informes')->with($data);
This is my table in the blade file
<table class="table table-striped table-bordered table-hover" id="myTable">
<thead>
<tr>
<th class="text-center">Vehiculo vendido</th>
<th class="text-center">Precio de compra</th>
<th class="text-center">Precio de venta</th>
<th class="text-center">Ganancia %</th>
<th class="text-center">Usuario</th>
<th class="text-center">Cliente</th>
<th class="text-center">Fecha venta</th>
</tr>
</thead>
<tbody>
#foreach($venta as $ventas)
<tr class="text-center">
#foreach($ventas->traerDesc as $descripcion)
<td>{{$descripcion->marca}}</td>
#endforeach
<td>{{$ventas->traerVehiculo->precioCompra}}</td>
<td>{{$ventas->traerVehiculo->precioVenta}}</td>
<td>asd</td>
<td>{{$ventas->traerUsuario->nombre.' '.$ventas->traerUsuario->apellidoPaterno.' '.$ventas->traerUsuario->apellidoMaterno}}</td>
<td>asd</td>
<td>{{date('d-m-Y', strtotime($ventas->traerVenta->fechaVenta))}}</td>
</tr>
#endforeach
</tbody>
</table>
This is my table accion. As you can see . It has 3 rows where accion = venta and all of them has the same info in all the tables. But it only brings the first row rejecting the others
And the view shows only 1 marca that would be like brand in english.
Hope can make myself clear enough. Thank you
Not sure it will help, but you use eager loading method with wrong way.
Right using is:
Model::with(relations)->where(condition)->get();
Or
Model::where(condition)->get()->load(relations);