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
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 have this code to view all invoices in my page:
#foreach ($user->invoices() as $invoice)
<tr>
<td>{{ $invoice->date()->toFormattedDateString() }}</td>
<td>{{ $invoice->total() }}</td>
<td>
Download
</td>
</tr>
#endforeach
However, the line $invoice->date()->toFormattedDateString() gives an error:
DateTime::__construct(): Failed to parse time string (#) at position 0 (#): Unexpected character
I just followed all instructions in the Laravel Cashier (Billing) website and I am getting this error. I have tried two versions of Laravel (5.8 and 5.5) and two Laravel Cashier versions (8.0 and 7.0). All tests have the same date issue.
I have added the correct migrations fields. This is how I insert/add subscription to my database:
$user = Auth::user();
$user->newSubscription('eat', 'eat_monthly')->create($request->stripeToken);
instead of {{ $invoice->date()->toFormattedDateString() }}
Can you try {{ $invoice->created_at->toFormattedDateString() }} ?
Stripe has changed date parament to created works.
Use this way:{{ $invoice->created }}
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
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