Using Laravel and Revisionable package. I am populating a table with user record modifications and have the following code snippet:
<tr>
<td width="150">{{ $revision->updated_at }}</td>
<td>User</td>
<td>{{ $revision->revisionable->first_name }} {{ $revision->revisionable->last_name }}</td>
<td width="50">{{ $revision->fieldName() }}</td>
<td width="50">{{ $revision->userResponsible()->first_name }}</td>
<td>{{ $revision->oldValue() }}</td>
<td>{{ $revision->newValue() }}</td>
</tr>
Getting the users first and last name works fine when the original user record exists but in some cases the original record (user) is deleted and it causes an error because the user record no longer exists. Is there an IF Statement I can use to first check if the first name field returns an error? Then I can display first/last name for records that still exist and something else if it doesn't exist.
Thanks in advance!
Blade has the orsyntax, really nice:
{{ $revision->revisionable->first_name or 'N/A'}}
{{ $revision->revisionable->last_name or 'N/A'}}
alternatives, though not as elegant:
{{ isset($revision->revisionable->first_name) ? $revision->revisionable->first_name : 'N/A' }}
or
#if (isset($revision->revisionable->first_name))
{{ $revision->revisionable->first_name }}
#else
N/A
#endif
Related
I'm having trouble on passing application_id from application#edit controller to addapplication#index controller. Please help me.
application#edit controller
$application->application_id = $request->application_id;
$application->save();
return redirect(route('addapplication.index'))->with('application_id',$request->application_id);
addapplication#index controller
$application_id = Session::get('application_id');
$applications = addapplication::where('application_id', $application_id)->get();
return view('user.addapplication.index',compact('applications'));
blade.php
#foreach ($applications as $application)
<tr>
<td>{{ $loop->index +1 }}</td>
<td>{{ $application->application_id }}</td>
<td>{{ $application->created_at }}</td>
<td>{{ $application->surname }}, {{ $application->firstname }} {{ $application->middlename }}</td>
<td><img src = {{ asset('assets/img/eye.ico') }}/>
</td>
</tr>
#endforeach
I'm getting this error:
Missing required parameters for [Route: addapplication.edit] [URI:
addapplication/{addapplication}/edit]. (View: D:\path\index.blade.php)
here is my route.php
Route::resource('application', 'ApplicationController');
Route::resource('addapplication', 'AddApplicationController');
Please help me. Thank you!
You have to use like this
{{route('addapplication.edit',['addapplication'=>$application->id])}}
The route helper requires an array as the secondary argument.
{{ route('addapplication.edit', ['addapplication' => $application->id])}}
You can figure out the required key (in any scenario) by using the artisan route:list command and looking at the values between the curly braces in the URI column.
In your case the route should look like this:
/addapplications/{addapplication}/edit
I'm trying to get the properties from the array returned by get method of Eloquent. It is a nested array as you see in image
I have tried the following answers from stackoverflow.
Property [title] does not exist on this collection instance ,
Trying to get property of non-object (View: C:\xampp\htdocs\travel\resources\views\user\profile.blade.php)
Looping PHP Nested Arrays - Extract values into Blade Views (Laravel)
My blade code is
#if(isset($reportRP))
#foreach($reportRP as $rrp)
<tr>
<td>{{ $rrp['name'] }}</td>
<td>{{$rrp['reports']->id }}</td>
#endforeach
#endif
Maybe:
#if(isset($reportRP))
#foreach($reportRP as $rrp)
<tr>
<td>{{ $rrp['name'] }}</td>
#foreach($rrp['reports'] as $report)
<td>{{ $report['id'] }}</td>
#endforeach
</tr>
#endforeach
#endif
$rrp['reports'] is an array of items. You'll have to specify from which object you'll want the id property
Anyway you can use Laravel function optional and if your object is empty it will return null
$rrp['name']->name ----- Will return error if $rrp['name'] is null
optional($rrp['name'])->name ----- Will return null if $rrp['name'] is null
I am working with laravel. I have two table. one is users and another is permission. I have two foreach loop. One is to show user data. User data coming from users table. And another foreach loop is is for checking is that user available or not in permission table.
I am fetching all user from users table like this:
$allTeachers = User::where('role',2)->paginate(4);
And am also fetching all data from permission table like this.
$allpermiteds = Specialpermission::where('add_teacher','!=','NULL')->get();
I have sent those two query to my view. And it is working fine.
Now I am printing those users data in my view like this
#foreach($allteachers as $teacher)
<tr>
<td>{{ $teacher->id}}</td>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->email }}</td>
<td><button>give</button></td>
<td><button>Remove</button></td>
#endforeach
I have printed two button there give and remove. Now I want to do that, If $teacher->id is available in the permission table add_teacher column then it should show remove button. Otherwise it should Show add button.
I have done like this, But it is not working properly.
#foreach($allteachers as $teacher)
<tr>
<td>{{ $teacher->id}}</td>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->email }}</td>
<td>#foreach($allpermitted as $p)
#if($p->add_teacher == $teacher->id)
<button>Remove</button>
#else
<button>give</button>
#endif
#endforeach</td>
#endforeach
It shows a lot of button. If there are 4 users in the permission table then it will show 4 button for each user. That is the main problem
Thanks in advance.
Simple, select add_teacher column from db, then convert your Specialpermission objects into array like below:
$permissionData = Specialpermission::where('add_teacher','!=','NULL')->select('add_teacher')->get();
$allpermiteds = collect($permissionData )->map(function($x){ return (array) $x; })->toArray();
Then use in_array to check for permission check
#foreach($allteachers as $teacher)
<tr>
<td>{{ $teacher->id}}</td>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->email }}</td>
<td>
#if(in_array($teacher->id,$allpermitted))
<button>Remove</button>
#else
<button>give</button>
#endif
</td>
#endforeach
I prefer using the eloquent function with that defines the relationship between the two tables:
$allTeachers = User::where('role',2)->with('SpecialPermisson')->paginate(4);
It will add a field of SpecialPermission (if it has). If not, it will give you a blank field, so check if its empty.
We have a project upgraded from laravel v 5.1 to v 5.4
and many issues and bugs appear after upgrade anyway have this one
Trying to get property of non-object for index.blade.php
and this is the code
<tbody>
#foreach($routeFEmails as $routeFEmail)
<tr>
<td>{{ $routeFEmail->id }}</td>
<td>{{ $routeFEmail->routeF->id }} ({{ $routeFEmail->routeF->vessel_name }})</td>
<td>{{ $routeFEmail->creator->type }}: {{ $routeFEmail->creator->first_name }} {{ $routeFEmail->creator->last_name }}</td>
<td>{{ $routeFEmail->status }}</td>
<td>{{ $routeFEmail->created_at->format('Y-m-d H:i') }}</td>
</tr>
#endforeach
</tbody>
i check model and no value from what mention is null no NULL
i did also
php artisan cache:clear
php artisan route:clear
php artisan view:clear
and this is a function in co controller
public function index()
{
$routeFEmails = RouteFEmail::orderBy('id', 'desc')->paginate(10);
return view('backend.route_f_emails.index', compact('routeFEmails'));
}
HOW I CAN FIX THIS ? :(
Your problem is with compact(), Return type of compact() is array [Read Here], so when you are passing compact('routeFEmails') it is converting object to array.
In index.blade.php you are accessing values of $routeFEmails as object. So you can try one of below solutions.
Solution 1
Write <td>{{ $routeFEmail['id'] }}</td> instead of <td>{{ $routeFEmail->id }}</td>
OR
Solution 2
Write
return view('backend.route_f_emails.index', ['routeFEmails'=>$routeFEmails]);
instead of
return view('backend.route_f_emails.index', compact('routeFEmails'));
trying to get a property on a non object means that the $routeFEmail is not an object not that some of it's properties like id is null.
Also the part
$routeFEmail->routeF
could be null ( My guess that the problem is there)
you can skip it with
#if( !is_null($routeFEmail->routeF) )
write something
#endif
or even dd if it is null
I have a report page that loops through a collection and displays the data in a generic bootstrap template table - it's showing data about users, and one user can have multiple rows in the table. I group the rows by the user ID and I want to basically stripe the table rows based on the user ID (not just alternating rows). So, for example, I might display 4 rows for user ID = 5 with a white background and then 2 rows for user ID = 6 with a gray background. I'd just use a local variable in straight php, but is there an elegant/clean way to do this in the blade templates?
#foreach($payments->getDetails() AS $k=>$detail)
<tr>
<td>{{ $detail->payment->userid }}</td>
<td>${{ $detail->payment->amount }}</td>
<td>{{ $detail->payment->status }}</td>
</tr>
#endforeach
In this example, $payments is a collection of $paymentsDetails objects. So based on the $detail->payment->userid value I'd like to determine the class or background color.
Thank you for any help in this.
Just a thought... This assumes the rows are ordered by user id naturally (it would not work otherwise in your example anyway...)... So here's the thought...
If the user id is odd, then give it a color, if it is even, give it the alternate color... that would work... Sort of ...
#foreach($payments->getDetails() AS $k=>$detail)
<tr>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->userid }}</td>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->amount }}</td>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->status }}</td>
</tr>
#endforeach
Something like this would also work... not sure I like it though
#foreach($payments->getDetails() AS $k=>$detail)
#if($detail->payment->userid % 2 == 0)
<tr>
<td class="evencolor">{{ $detail->payment->userid }}</td>
<td class="evencolor">>${{ $detail->payment->amount }}</td>
<td class="evencolor">{{ $detail->payment->status }}</td>
</tr>
#else
<tr>
<td class="oddcolor">{{ $detail->payment->userid }}</td>
<td class="oddcolor">${{ $detail->payment->amount }}</td>
<td class="oddcolor">{{ $detail->payment->status }}</td>
</tr>
#endif
#endforeach
Anyway... just a possible direction... someone else might have a more elegant idea...
I got the solution to this via www.laracasts.com from user #Cronix:
I'd do it the way you were thinking originally and just use
#php/#endphp blade directives to determine if the userid has changed
during each loop iteration and if it does change the color.
#php
$lastid = null;
$rowclass = 'grey';
#endphp
#foreach($payments->getDetails() AS $k=>$detail)
#php
//if userid changed from last iteration, store new userid and change color
if ($lastid !== $detail->payment->userid)
{
$lastid = $detail->payment->userid;
if ($rowclass == 'grey') $rowclass = 'white';
else $rowclass = 'grey';
}
#endphp
<tr class="{{ $rowclass }}">
<td>{{ $detail->payment->userid }}</td>
<td>${{ $detail->payment->amount }}</td>
<td>{{ $detail->payment->status }}</td>
</tr>
#endforeach
If anyone out there has a more elegant Blade-focused idea on how to solve it, I'd love to hear about it.
hey guys the methods above failed for me, incase anyone els needs this : im using laravel 5.4 with php 5.6===> this is what has worked for me;
#php
if ( $post->dbdata == 'approved'):
$color = 'green';
elseif ( $post->dbdata == 'Pending'):
$color = 'yellow';
elseif ( $post->dbdata == 'rejected'):
$color = 'red';
else:
$color = 'black';
endif;
#endphp
<tr style="background-color: {{$color}}">
<td>
<h6>{{ $post->dbdata }}</h6>
</td>
</tr>