print readable message for tiny int value - php

I have a column PAID tinyint(1). Now I want to display this value 0/1 as UNPAID/PAID in view page table. How should I do this?
Also if the PAID column data is PAID then color it as green and if UNPAID then color it as BLUE. How can I do this?
controller code block to fetch data
function fetchData() {
$ordered_books = OrderedBook::orderBy('id', 'DESC')->get()->toArray();
return compact('ordered_books');
}
view page table code block
<table id="showBooksIn" class="table table-bordered gridview">
<thead>
<tr>
<th>BOOK ID</th>
<th>BILLED DATE</th>
<th>BILLED NUMBER</th>
<th>QUANTITY</th>
<th>PRICE</th>
<th>PAID</th>
<th>REMARKS</th>
</tr>
</thead>
<tbody>
#foreach($ordered_books as $data)
<tr>
<td> {{$data['BookID']}} </td>
<td> {{$data['BilledDate']}} </td>
<td> {{$data['BilledNum']}} </td>
<td> {{$data['Qunatity']}} </td>
<td> {{$data['Price']}} </td>
<td> {{$data->bill_paid}} </td>
<td> {{$data['Remarks']}} </td>
</tr>
#endforeach
</tbody>
</table>
As suggested I edited my model class
class OrderedBook extends Model
{
//
protected $appends = ['bill_paid'];
public function getBillPaidAttribute(){
if($this->BillPaid == 0){
return 'UNPAID';
}else {
return 'PAID';
}
}
}
Now getting error as :: ErrorException (E_NOTICE) Undefined property: App\OrderedBook::$BillPaid

Use if conditions in your blade template and apply css for coloring.
#foreach($ordered_books as $data)
<tr>
<td> {{$data['BookID']}} </td>
<td> {{$data['BilledDate']}} </td>
<td> {{$data['BilledNum']}} </td>
<td> {{$data['Qunatity']}} </td>
<td> {{$data['Price']}} </td>
#if($data['BillPaid'] === 0)
<td style='color:blue;'> UNPAID </td>
#else
<td style='color:green;'> PAID </td>
#endif
<td> {{$data['Remarks']}} </td>
</tr>
#endforeach

make Eloquent: Mutators in OrderBook
protected $appends = ['bill_status']
public function getBillStatusAttribute(){
if($this->BillPaid == 0){
return 'unpaid';
}else {
return 'paid';
}
}
in view
$orderBook->bill_status

Related

Nested foreach loop creating duplicate records with if condiotion - PHP Laravel 8

Problem: There are modules, users, and user_modules tables, where the admin can assign multiple modules with permissions to a user. Admin can update module's permission which is already assigned to that user, and the modules which are not assigned should be loaded on blade view on the same table.
But the issue is data is duplicating
I am posting my code with images
AdminController:
$modules = Module::all();
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules'));
seller_assign_modules.blade.php
<table class="table table-striped">
<thead>
<tr>
<th>Modules</th>
<th>Add</th>
<th>Edit</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach ($user_modules as $user_mod)
#foreach ($modules as $mod)
#if ($mod->id == $user_mod->module_id)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#else
<tr>
<td scope="row">{{$mod->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endif
#endforeach
#endforeach
</tbody>
</table>
modules table:
user_modules table:
result on seller_assign_modules.blade.php
I NEED THIS:
You can do something like below (this is not a good approach however)
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
//Get modules where user dont have records
$non_user_modules = Module::whereNotIn('id', (clone $user_modules)->pluck('module_id')->toArray())->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules','non_user_modules'));
Then in the blade
#foreach ($user_modules as $user_mod)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#endforeach
#foreach ($non_user_modules as $non_user_module)
<tr>
<td scope="row">{{$non_user_module->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endforeach
Best approach you could do
Make a relationship at Module model like userModules()
Then get Modules like below
$modulesWithUser = Module::with(['userModules' => function($userQuery){
$userQuery->where('user_id',$user_id);
}])->get();
Then loop $modulesWithUser in blade and access its relationship inside loop to show users permissions.
The problem with here is,Module has manyUser_modules, but you only need one.
So in this case, one approach is map a new property like user_module to each Module.
AdminController
$modules = Module::all();
$userModeules = User_module::where('user_id', $user->id)->get();
$modules = $modules->map(function($module) {
$module->user_module = $userModules->firstWhere('module_id', $module->id);
return $module;
});
return view(
'admin/seller_assign_modules',
compact('modules')
);
seller_assign_modules.blade.php
<tbody>
#foreach($mdules as $module)
<tr>
<td scope="row">{{ $module->name }}</td>
<td scope="row">{{ optional($module->user_module)->add }}</td>
<td scope="row">{{ optional($module->user_module)->edit }}</td>
<td scope="row">{{ optional($module->user_module)->view }}</td>
<td scope="row">{{ optional($module->user_module)->del }}</td>
</tr>
#endforeach
</tbody>

Laravel error : Trying to get property of non-object in show.blade.php

My code is here if you want any other section of code, i will provide also that piece of code
<div class="panel-heading ">Organization Profile</div>
<table class="table table-striped">
<thead>
<tr>
<th>Attributes</th>
<th>Values</th>
</tr>
</thead>
<tbody>
<tr>
<td>Org Name</td>
<td>{{$org->name}}</td>
</tr>
<tr>
<td>Owner Name</td>
<td>{{$org->owner}}</td>
</tr>
</tbody>
</table>
</div>
Trying to get property of non-object issue usually comes in Laravel when you are returning array but try using object in your blade file or not passing the correct object string at all.
I notice from comments that you are passing $orgs, not $org in the function.
public function create() {
return view('Admin.organizations.create',compact('orgs'));
}
Update your blade file like below:
<div class="panel-heading ">Organization Profile</div>
<table class="table table-striped">
<thead>
<tr>
<th>Attributes</th>
<th>Values</th>
</tr>
</thead>
<tbody>
<tr>
<td>Org Name</td>
<td>{{$orgs->name}}</td>
</tr>
<tr>
<td>Owner Name</td>
<td>{{$orgs->owner}}</td>
</tr>
Considering all the comments, you should probably have something like this in your controller:
public function show($id)
{
$org = Organization::find($id);
// $data = ['org', $id];
return view('Admin.organizations.show')->with(compact('org'));
}
and this is your blade view:
<div class="panel-heading ">Organization Profile</div>
#if (empty($org))
<h1>Organization is empty</h1>
#else
<table class="table table-striped">
<thead>
<tr>
<th>Attributes</th>
<th>Values</th>
</tr>
</thead>
<tbody>
<tr>
<td>Org Name</td>
<td>{{$org->name}}</td>
</tr>
<tr>
<td>Owner Name</td>
<td>{{$org->owner}}</td>
</tr>
</tbody>
</table>
#endif
</div>
Of course not knowing what is in the Organization model would keep us from knowing whether owner and name exist as properties of the model.

Trying to get property of non-object Laravel 5.4

Hi Guy's Im always getting an error from laravel 5.4.
Im creating a list of members... but when i use #foreach it says
ErrorException in 6163a8b030c7474bc8eaad359ab99eb61ebdb127.php line 38:
Trying to get property of non-object (View: E:\wamp64\www\gplspring2017\resources\views\admin\memberlist.blade.php)
Here's my controller
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->first();
$count = count($teams);
if (!$count) {
return redirect('404');
} else {
return view('/admin/memberlist', ['team' => $teams]);
}
}
and here's my view code:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($qmember as $get_member)
<tr>
<td><img src="{{ $get_member->member_pic }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>
When i remove the foreach the code is working.. but i try to add the #foreach($qmember as $get_member) it not working anymore...
Check this line:
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
when you are using first() then it does not return an Std Class object. If you want it so then use get()
Using first() method you are fetching only one record from DB that match with the ID (First one matched). You need to use get().
Controller:
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->get(); //Use get() if you expect more than one result not first()
$count = count($teams);
if(! $count)
{
return redirect('404');
}
else
{
return view('/admin/memberlist', ['teams' => $teams]);// this team variable must be used in the blade
}
}
View:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($teams as $team)
<tr>
<td><img src="{{ $team['member_pic'] }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>

How to sum up total in an index view in Laravel

I'm working on an invoice system in a homemade CRM. I try to sum up the price for all of my items to get the grand total.
I don't know how to get the grandtotal in my index view.
Here is my table in factures/show.blade.php
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Description</th>
<th class="text-right">Prix</th>
</tr>
</thead>
<tbody>
#foreach($facture->items as $item)
<tr>
<td>{{$item->description}}</td>
<td class="text-right">{{$item->prix}} $</td>
</tr>
#endforeach
<tr>
<td class="text-right">TPS</td>
<td class="text-right">{{ $item->calcultps() }} $</td>
</tr>
<tr>
<td class="text-right">TVQ</td>
<td class="text-right">{{ $item->calcultvq() }} $</td>
</tr>
<tr>
<td class="text-right">Total</td>
<td class="text-right">{{ $item->calculgrandtotal() }} $</td>
</tr>
</tbody>
</table>
and here is my table in factures/index.blade.php
<table class="table table-striped">
<thead><tr>
<th><p># de facture</p></th>
<th><p>Date</p></th>
<th><p>Description</p></th>
<th><p>Client</p></th>
<th class="text-right"><p>Montant</p></th>
<th class="text-right"><p>TPS</p></th>
<th class="text-right"><p>TVQ</p></th>
<th class="text-right"><p>Grand total</p></th>
</tr></thead>
#foreach($factures as $facture)
<tr>
<td>{{$facture->id}}</td>
<td>{{ $facture->created_at->format('d F Y') }}</td>
<td>{{ $facture->courtedescription}}</td>
<td>{{ $facture->client->prenom}} {{ $facture->client->nom}}</td>
<td class="text-right">{{ $facture->montant}} $</td>
<td class="text-right">{{ $facture->tps}} $</td>
<td class="text-right">{{ $facture->tvq}} $</td>
<td class="text-right"> IDONTKNOW $</td>
</tr>
#endforeach
</table>
here is my Item.php model
use Illuminate\Database\Eloquent\Collection;
class Item extends \Eloquent {
protected $fillable = ['nom', 'prix'];
public function facture()
{
return $this->belongsTo('Facture');
}
public function calcultps(){
$total = DB::table('items')->sum('prix');
$tps = $total *0.05;
echo $tps;
}
public function calcultvq(){
$total = DB::table('items')->sum('prix');
$tvq = $total *0.09975;
echo $tvq;
}
public function calculgrandtotal(){
$totalsanstaxes = DB::table('items')->sum('prix');
$tps = $totalsanstaxes *0.05;
$tvq = $totalsanstaxes *0.09975;
echo $totalsanstaxes+$tps+$tvq;
}
}
and my Facture.php model
use Illuminate\Database\Eloquent\Collection;
class Facture extends \Eloquent {
protected $fillable = [];
public function client()
{
return $this->belongsTo('Client');
}
public function items(){
return $this->hasMany('Item', 'facture_id');
}
}
my index function for my FacturesController.php
public function index()
{
$factures = Facture::all();
return View::make('factures.index')->withFactures($factures);
}
my show function for my FacturesController.php
public function show($id)
{
$facture = Facture::find($id);
return View::make('factures.show')->withFacture($facture);
}
I tried these (in the factures/index.blade.php) without success:
{{ $item->calculgrandtotal() }}
{{ $items->calculgrandtotal() }}
{{ $facture->items->calculgrandtotal() }}
and this returns an array
{{$facture->items}}
but this doesn't work
{{$facture->items->prix}}
I know there is something I don't understand here. and I wonder how to print out the grandtotal in my index view for each of the invoice?
I used the comment of #MattBurrow (thank you) and I managed to print out what I want using this:
index.blade.php
{{ Item::calculgrandtotal() }}
Item.php model
static function calculgrandtotal(){
$totalsanstaxes = DB::table('items')->sum('prix');
$tps = $totalsanstaxes *0.05;
$tvq = $totalsanstaxes *0.09975;
return $totalsanstaxes+$tps+$tvq;
}

Clickable row with link

How can I make each row clickable without repeating
This one is an example that shows the problem, parameter could be the code:
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr>
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr>
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
Thanks
Excuse me for my English, I hope that you understand the problem.
There are a few different ways to achieve this. Here are a couple using plain javascript and one using jQuery.
Plain JS
With plain javascript with just use the onclick parameter. Pretty straight forward.
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr onclick="window.location='page/parameter1';">
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr onclick="window.location='page/parameter2';">
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
</table>
jQuery
With jQuery you add a class so you can use that as the selector. There is also a data-href parameter that will hold the URL you want the user to go to when they click the row.
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr class="clickable" data-href="page/parameter1">
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr class="clickable" data-href="page/parameter2">
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
</table>
<script>
jQuery(document).ready(function($) {
$("tr.clickable").click(function() {
window.location = $(this).data("href");
});
});
</script>
Your code should look like :
<table>
<thead>
<tr>
<th>Code</th>
<th>User</th>
...
</tr>
</thead>
<tbody>
<tr>
<td> 123 </td>
<td> User A </td>
...
</tr>
<tr>
<td> 456 </td>
<td> User B </td>
...
</tr>
</tbody>
Added end tag </a>

Categories