Trying to get property of non-object laravel 5.4 - laravel-5

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

Related

Missing required parameters for [Route: addapplication.edit]

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

Date Issue in Laravel Cashier Invoice

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 }}

Property [id] does not exist on this collection instance. (View

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

Laravel with Revisionable Package and catching errors

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

laravel 4 - empty routes via Artisan::call()

If I use the following:
Artisan::call('routes')
It returns 0.
I was hoping to parse the same routes that appears when I run php artisan routes from CLI.
Has anyone tried this with success?
You dont need artisan - just use the routes class itself:
<table>
<tr>
<th>Routes</th>
</tr>
#foreach(Route::getRoutes() as $name => $route)
<tr>
<td>{{ $name }}</td>
</tr>
#endforeach
</table>

Resources