Trying to get property of non-object Laravel 5.4 - php

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>

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>

print readable message for tiny int value

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

How to iterate an array cleanly PHP

Hello im having a hard time iterating my array. I don't know what to use a simple foreach or with a foreach with $key. I tried with key but im having an error of:
Illegal string offset 'payroll_employee_company_id' (View: /var/www/html/digimahouse/resources/views/member/payrollreport/loan_summary_table.blade.php)
How can i iterate it successfully?
here's my array
here's my foreach
#foreach($totals as $key => $total)
#if($total['payroll_employee_company_id'] == $comid->payroll_company_id)
<tr class="total">
<td class="text-center"><strong>TOTAL</strong></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center">{{$total['loan_total']}}</td>
<td class="text-center">{{$total['total_total_payment']}}</td>
<td class="text-center">{{$total['total_remaining_balance']}}</td>
<td class="text-center"></td>
</tr>
#endif
#endforeach
here's my controller
public function loan_summary()
{
$data["page"] = "Loan Summary";
$data["_loan_data"] = PayrollDeductionController::get_deduction($this->shop_id());
$data["_company"] = Payroll::company_heirarchy(Self::shop_id());//Tbl_payroll_company::where("shop_id", Self::shop_id())->where('payroll_parent_company_id', 0)->get();
$data['totals'] = $this->get_totals_loan_summary($data);
return view("member.payrollreport.loan_summary", $data);
}
You've said the picture is result for {{ dd($totals) }}. In this case, do this:
#foreach($totals['totals'] as $key => $total)
#if($total['payroll_employee_company_id'] == $comid->payroll_company_id)
<tr class="total">
<td class="text-center"><strong>TOTAL</strong></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center">{{$total['loan_total']}}</td>
<td class="text-center">{{$total['total_total_payment']}}</td>
<td class="text-center">{{$total['total_remaining_balance']}}</td>
<td class="text-center"></td>
</tr>
#endif
#endforeach
you foreach was not on the correct array
#foreach($data['totals'] as $key => $total)
#if($total['payroll_employee_company_id'] == $comid->payroll_company_id)
<tr class="total">
<td class="text-center"><strong>TOTAL</strong></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center">{{$total['loan_total']}}</td>
<td class="text-center">{{$total['total_total_payment']}}</td>
<td class="text-center">{{$total['total_remaining_balance']}}</td>
<td class="text-center"></td>
</tr>
#endif
#endforeach

Call to a member function links() on array in Laravel error

I was working with Laravel and I am trying to paginate my table of books. I got this error "Call to a member function links() on array in Laravel". It may be as the duplicate's error, but I still can't figure out how to solve my thing.
BookController fiddle:
public function index()
{
$books = Book::simplePaginate(3)->all();
$authors = Author::all();
$genres = Genre::all();
return view('books.all')->withBooks($books)->withAuthors($authors)->withGenres($genres);
}
books/all.blade.php fiddle
<table class="table table-hover">
<tr class="info">
<td>#</td>
<td>Name</td>
<td>Author</td>
<td><center>Visit</center></td>
</tr>
#foreach($books as $book)
<tr>
<td width="50px"><img width="50px" height="75px" src="{{ asset('images/' . $book->image) }}"></td>
<td width="50px">{{ $book->name }}</td>
<td width="50px">{{ $book->author->name }}</td>
<td width="50px"><center><button class="btn btn-success">Visit</button></td>
</tr>
#endforeach
</table>
{{ $books->links() }}
Just remove the ->all() method from the $books variable.
$books = Book::paginate(10);
paginate() function considers taking all content from the table, which is taken here by Book model

Update multiple data from <select> <option> in database using laravel 5.1

i have some coding, i wanna to update multiple data from select option value in database using laravel 5.1. i'm using ajax onchange="form.submit()", so i can update data without submit.
this is my view
{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
<table class="dataTable" id="table-user">
<thead class="grey lighten-3">
<tr>
<td class="center-align no-sort">Photo</td>
<td class="center-align">Fullname</td>
<td class="center-align">Rule</td>
<td class="center-align">Action</td>
</tr>
</thead>
<tbody>
<?php $hitung = $UserAccess->count(); ?>
#foreach($UserAccess as $list)
<tr>
<td class="center-align" width="50">
#if($list->avatar == NULL)
<img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
#else
<img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
#endif
</td>
<td class="red1-text lato-bold center-align">{{$list->name}}</td>
<td class="center-align" width="150">
<select id="selectrole" name="selectrole" onchange="form.submit()">
#foreach($UserAccessRole as $listRole)
<option value="{{$listRole->id}}" #if($list->user_access_role_id_fk == $listRole->id) selected="selected"#endif>{{$listRole->name}}</option>
#endforeach
</select>
</td>
<input type = "hidden" value = "{{$list->id}}" name = "idmain">
#if($hitung > 2)
<td class="center-align">
<a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
</td>
#else
<td class="center-align">Remove Access</td>
#endif
</tr>
#endforeach
</tbody>
</table>
{!! Form::close() !!}
this is my controller
public function doUpdateAccessRole(Request $request)
{
$UserAccess = UserAccess::orderBy('name', 'asc')->get();
$id_main = $request->input('idmain');
$id_role = $request->input('selectrole');
$Role = UserAccess::findOrFail($id_main);
$Role->user_access_role_id_fk = $id_role;
$Role->update($request->all());
return redirect('setting/useraccess');
}
this is my route
Route::post('setting/updaterole',['uses'=>'SettingController#doUpdateAccessRole','as'=>'updateaccessrole']);
using my coding update function already work, but just last id has been update. please help me, thank you
my code already work. just add foreach in my controller. like this, my controller :
public function doUpdateAccessRole(Request $request)
{
$UserAccess = UserAccess::all();
foreach($UserAccess as $list)
{
$id_main = $request->input('idmain'.$list->id);
$id_role = $request->input('selectrole'.$list->id);
$Role = UserAccess::find($id_main);
$Role->user_access_role_id_fk = $id_role;
$Role->update($request->all());
}
return redirect('setting/useraccess');
}
and this my view :
{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
<table class="dataTable" id="table-user">
<thead class="grey lighten-3">
<tr>
<td class="center-align no-sort">Photo</td>
<td class="center-align">Fullname</td>
<td class="center-align">Rule</td>
<td class="center-align">Action</td>
</tr>
</thead>
<tbody>
<?php $hitung = $UserAccess->count(); ?>
#foreach($UserAccess as $list)
<tr>
<td class="center-align" width="50">
#if($list->avatar == NULL)
<img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
#else
<img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
#endif
</td>
<td class="red1-text lato-bold center-align">{{$list->name}}</td>
<td class="center-align" width="150">
<select id="selectrole" name="selectrole{{$list->id}}" onchange="form.submit()">
#foreach($UserAccessRole as $listRole)
<option value="{{$listRole->id}}" #if($list->user_access_role_id_fk == $listRole->id) selected="selected"#endif>{{$listRole->name}}</option>
#endforeach
</select>
</td>
<input type = "hidden" value = "{{$list->id}}" name = "idmain{{$list->id}}">
#if($hitung > 2)
<td class="center-align">
<a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
</td>
#else
<td class="center-align">Remove Access</td>
#endif
</tr>
#endforeach
</tbody>
</table>
{!! Form::close() !!}
and now my coding already work.

Categories