Thank you for all the help in the past.
On my app, when i try to view a certain blade, it returns the error
"Trying to get property of non-object".
It has pointed me to the blade and the section of code where the error comes from:
#extends('layouts.app')
#section('content')
#component('dashboard.frame')
<div class="panel panel-default">
<div class="panel-heading">
Create
Activation Pins - {{ $item->name }}
</div>
{{--<div class="panel-body">--}}
{{--#if($pin_item && !$pin_item->pivot->is_paid )--}}
{{--<form action="/activation-pins/{{ $pin_item->id }}" method="post">--}}
{{--<input type="hidden" name="_method" value="PUT">--}}
{{--<input type="hidden" name="vendor_id" value="{{ $item->vendor->id }}">--}}
{{--{!! csrf_field() !!}--}}
{{--<button type="submit" class="btn btn-success">Mark As Paid</button>--}}
{{--</form>--}}
{{--#endif--}}
{{--</div>--}}
<table class="table">
<thead>
<tr>
<th>S/N</th>
<th>Name</th>
<th>Is Active</th>
<th>User</th>
</tr>
</thead>
<tbody>
#forelse($item->activation_pins as $itm)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $itm->pin }}</td>
<td>
<span class="label label-{{ $itm->is_active ? ( $itm->is_valid ? 'info' : 'danger' ) : 'warning' }}">
{{ $itm->is_active ? ( $itm->is_valid ? 'active' : 'expired' ) : 'inactive' }}
</span>
</td>
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
</tr>
#empty
<tr>
<td colspan="6">
<p class="text-center">No Activation Pins</p>
</td>
</tr>
#endforelse
</tbody>
</table>
</div>
#endcomponent
#endsection
With emphasis on:
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
Could I be missing something? Thanks.
This is what it displays now:
Related
just wondering how to format this output for a better view? (second picture). as you can see in the first picture the output from pluck('name') was like {Tidak Masuk Sekolah} {Terlambat}. check the pictures below
and this is my view, I'm using table
<div class="card-body">
<h2 class="display-4 text-center">{{ $title }}</h2>
<div class="table-responsive d-flex justify-content-center">
<table class="table mx-9 my-6 table-borderless">
<tr>
<td>Nama Siswa</td>
<td>{{ $counseling->student->name }}</td>
</tr>
<tr>
<td>Kelas</td>
<td>{{ $counseling->student->student_class->name }}</td>
</tr>
<tr>
<td>Masalah yang dihadapi</td>
#foreach ($counseling->problems->pluck('name') as $problem_name) {
<td>{{ $problem_name }}</td>
}
#endforeach
</tr>
</table>
</div>
</div>
You could use an ol element to achieve that look.
<tr>
<td>
<p>Masalah yang dihadapi</p>
<ol>
#foreach ($counseling->problems->pluck('name') as $problem_name)
<li>{{ $problem_name }}</li>
#endforeach
</ol>
</td>
</tr>
Or
<tr>
<td>Masalah yang dihadapi</td>
<td>
<ol>
#foreach ($counseling->problems->pluck('name') as $problem_name)
<li>{{ $problem_name }}</li>
#endforeach
</ol>
</td>
</tr>
Depending on where you want the list to appear
This question already has answers here:
How to stop a loop PHP
(4 answers)
Short circuit Array.forEach like calling break
(30 answers)
How to break a foreach loop in laravel blade view?
(5 answers)
Closed 3 years ago.
I'm setting up a loop whenever i tried to print table first row print good with category name and then next row print table with ONE tr(row) data and closed the tag and again start new row and print next data with one row...
i want to print all data with respect to its category name without break multiple table
#if( ! empty($packages) )
#php
$serviceId=null;
#endphp
#foreach($packages as $package)
#if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $package->service->name }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId=$package->service->id
#endphp
#endif
#if($serviceId != $package->service_id)
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $package->service->name }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId = $package->service_id
#endphp
#endif
<!--SERVICE NAME TABLE-->
<div class="row">
<div class="col-sm-12">
<div class="well">
<div class="table-responsive">
<table class="table table-striped table-sm ">
<thead>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>#php $price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td>{{ $package->description }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
!--SERVICE NAME TABLE-->
#endforeach
#else
No Record Found
#endif
You can create an extra array to map this,
In method
$packagesNew = [];
foreach($packages as $k => $v)
{
$packagesNew[$v->service->name]['name'] = $v->service->name;
$packagesNew[$v->service->name]['id'] = $v->service->id;
$packagesNew[$v->service->name]['service_id'] = $v->service_id;
$packagesNew[$v->service->name]['data'][] = $v;
}
pass packagesNew to view
Then I made changes in view, please check.
#if( ! empty($packagesNew) )
#php
$serviceId=null;
#endphp
#foreach($packagesNew as $k => $package)
#if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $k }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId=$package['id'];
#endphp
#endif
#if($serviceId != $package['service_id'])
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $k }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId = $package['service_id'];
#endphp
#endif
<!--SERVICE NAME TABLE-->
<div class="row">
<div class="col-sm-12">
<div class="well">
<div class="table-responsive">
<table class="table table-striped table-sm ">
<thead>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
</thead>
<tbody>
#foreach($package['data'] as $k1 => $v1)
<tr>
<td>{{ $v1->id }}</td>
<td>{{ $v1->name }}</td>
<td>#php $price = isset($userPackagePrices[$v1->id]) ? $userPackagePrices[$v1->id] : $v1->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
<td>{{ $v1->minimum_quantity }}</td>
<td>{{ $v1->maximum_quantity }}</td>
<td>{{ $v1->description }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
!--SERVICE NAME TABLE-->
#endforeach
#else
No Record Found
#endif
The Routes
Route::get('/adminContacts/{user_id}', 'AdminContactsController#index')->name('adminContacts.index')->middleware('is_admin');
Route::get('/adminContacts/{user_id}/create', 'AdminContactsController#create')->name('adminContacts.create')->middleware('is_admin');
adminContactController
public function index($user_id)
{
// Confirm User exists
User::findOrFail($user_id);
$filter = request('filter', NULL);
$contacts = NULL;
if ($filter == NULL)
$contacts = Contacts::query()->where('owner_id', $user_id)->sortable()->paginate(5);
else
$contacts = Contacts::query()->where('owner_id', $user_id)->where('name', 'like', '%'.$filter.'%')
->orWhere('number', 'like', '%'.$filter.'%')
->sortable()->paginate(5);
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create($user_id)
{
return view('adminContacts.create')->withUserId($user_id);
}
index.blade.php
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index') }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index') }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
The error is:
Undefined variable: user_id (View: D:\laragon\www\SampleContacts\resources\views\adminContacts\index.blade.php)
Am I missing something? The idea is to pass the $user_id to the adminContact.create form where i can use it to set
$contact->owner_id = $user_id;
so the entered contact apears under that users table.
I don't think you can pass variables into your view like you're doing now.
In your controller, try changing this line:
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
To this:
return view('adminContacts.index')->with('contacts', $contacts)->with('user_id', $user_id);
You could explicitly name the variable when passing it to the view, either this way
return view('adminContacts.index')->with('user_id', $user_id);
Or
return view('adminContract.index', compact('user_id'));
I'm not sure what the name of the variable becomes when passed to the view via withUserId, but I'm guessing this is the issue.
Found the problem.
#extends('layouts.app')
#section('content')
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index', $user_id) }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index', $user_id) }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
#endsection
Anywhere where there is a {{ route('adminContacts.index') }} it also needed the $user_id to be passed along with it because I'm also using it in the routes as '/adminContacts/{user_id}'
Thank you for all the help.
I'm trying to call the timeStamp but it doesn't appear in my table. Here is my code:
#foreach($cevents as $cevent)
<tr >
<td class="col-lg-2">
{{ $cevent->name_client }}
</a>
</td>
<td class="col-lg-2">
{{ $cevent->name_event }}
</td>
<td class="col-lg-4">
{{ $cevent->details }}
</td>
<td class="col-lg-3">
{{ $cevent->created_at }}
</td>
</tr>
#endforeach
Using dd() in a controller I can find it, so the value is present.
How can I fix this?
I would like to display data based on the chosen values from select box and search box. but select box ( maybe search box has same issue too) get reset whenever I click the page 2,3,4....
I believe I missed something with pagination code in the view / controller.
Please let me know the way to solve this issue.
Controller
class MemberListTestController extends Controller
{
public function search(Request $request)
{
$request->flash();
$memberName = $request->input('memberName');
$chosenCenter = $request->input('chosenCenter');
$memberLists= DB::table('tbMbrMember as Member')
->select(['Org.orgNm',
'Member.firstNm',
'Member.lastNm',
'Member.regDate',
'MshipCd.mshipNm',
'Att.dt',
'Member.phoneCell',
'Member.email',
'Group.groupNm',
DB::raw('MAX(Att.dt) AttDt, GREATEST(MAX(Reg.endDt),MAX(Reg.expDt)) rExpDt')])
->join('tbMbrCenter as Center', 'Member.mbrCd', '=', 'Center.mbrCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->leftjoin('tbMbrGroup as Group','Center.orgCd','=','Group.orgCd')
->leftjoin('tbMbrMshipReg as Reg', 'Member.mbrCd', '=', 'Reg.mbrCd')
->leftjoin('tbCmMshipCd as MshipCd', 'MshipCd.mshipCd', '=', 'Reg.mshipCd')
->leftjoin('tbMbrAtt as Att', 'Att.mbrCd', '=', 'Member.mbrCd')
->where('Center.orgCd', $chosenCenter)
->groupby('Member.mbrCd')
->paginate(25);
$centerLists = DB::table('tbMbrCenter as Center')
->select('Org.orgNm','Center.orgCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->where('Center.isShow','1')
->where('Org.companyCd','c1')
->where('Org.isShowList','1')
->groupby('Center.orgCd')
->orderby('Org.orgNm')
->get();
return view('member.memberList_test')
-> with('memberLists', $memberLists)
-> with('centerLists', $centerLists);
}
}
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('member/test','MemberListTestController#search');
Route::post('member/test','MemberListTestController#search');
});
View
#extends('layouts.master')
#section('content')
<div class="container-fluid">
<p></p>
<div class="row"></div>
<div class="row">
<div class="col-md-4">
<form method="post" action="{{ url('member/test') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<select class="form-control" id="chosenCenter" name="chosenCenter">
<option value="" selected disabled> Choose Center</option>
#foreach ( $centerLists as $centerList )
<option value= {{ $centerList->orgCd }} #if (old('chosenCenter') == $centerList->orgCd) selected #endif > {{ $centerList->orgNm }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control"
placeholder="Search for First Name, Last Name... 'search all' for blank "
name="memberName">
<span class="input-group-btn">
<button class="btn btn-default " type="submit">Search</button>
</span>
</div>
</div>
</form>
</div>
</div>
<div class='row'></div>
<div class="row">
<table class="table table-striped table-condensed">
<thead>
<th>#</th>
<th>Center</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Reg.</th>
<th>Recent Membership</th>
<th>Date of Membership Exp.</th>
<th>Latest Attendance</th>
<th>Cell Phone</th>
<th>Email</th>
<th>Member Group</th>
</thead>
<tbody>
<?php $count = 1; ?>
#foreach($memberLists as $memberList)
<tr>
<td>{{ (($memberLists->currentPage() - 1 ) * $memberLists->perPage() ) + $count++}}</td>
<td> {{ $memberList->orgNm }}</td>
<td> {{ $memberList->firstNm }} </td>
<td> {{ $memberList->lastNm }} </td>
<td> {{ $memberList->regDate }} </td>
<td> {{ $memberList->mshipNm }}</td>
#if (($memberList->rExpDt) >= date('y-m-d'))
<td> {{$memberList->rExpDt }}</td>
#else
<td> Expired on {{ $memberList->rExpDt }} </td>
#endif
<td>{{ $memberList->AttDt }}</td>
<td> {{ $memberList->phoneCell }}</td>
<td> {{ $memberList->email }}</td>
<td> {{ $memberList->groupNm }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $memberLists->render() !!}
</div>
</div>
#stop
You have to pass the parameters via GET and recover the parameters from the URL.
{!! $memberLists->appends(Input::all())->render() !!}
This will send you to page 2 and so on... with all the inputs values.
I found a video in laracasts that explains all the built-in pagination options for your blade template in laravel 5.2 :
https://laracasts.com/lessons/crazy-simple-pagination
you will see that the correct way for a search page is:
{{ $memberLists->appends(Request::except('page'))->links() }}
This way it will keep the current url parameteres.