Date Issue in Laravel Cashier Invoice - laravel

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

Related

Laravel Foreach Nested or change my function?

I'm a newbie, studying programming.
Doing Laravel.
My table looks like this
ID FormID Type
1 1 Regional
2 1 Remote
3 1 Metro
4 2 Regional
5 2 Remote
6 3 Regional
7 3 Remote
My controller
$FormID = WorkArea::select('FormID', 'Type')->get();
My View is this
#foreach( $WorkArea as $WorkAreas)
<tr></tr>
<td> {{ $WorkAreas->FormID}} </td
<td> {{ $WorkAreas->Type}} </td>
#endforeach
Question:
How to foreach loop to show something like this in my VIEW
Form ID Type
1 Regional Remote Metro
2 Regional Remote
3 Regional Remote
Thank You!
You could use the Collection to help transform the structure for you:
$workAreas = WorkArea::get(['FormID', 'Type'])
->groupBy('FormID')
->map->pluck('Type');
#foreach ($workAreas as $FormID => $types)
<tr>
<td>{{ $FormID }}</td>
<td>{{ $types->join(' ') }}</td>
</tr>
#endforeach
Laravel 8.x Docs - Collections - Method groupBy
Laravel 8.x Docs - Collections - Method map
Laravel 8.x Docs - Collections - Method pluck
Laravel 8.x Docs - Collections - Method join
Laravel 8.x Docs - Collections - Higher Order Messages
try this
in your controller
$workAreas = WorkArea::select('FormID', 'Type')->get();
in your View
#if(! empty($workAreas))
#foreach($workAreas as $workArea)
<tr>
<th>{{ $work->FormID }}</th>
<th>
#foreach($workAreas as $work)
#if($workArea->FormID === $work->FormID)
{{ $work->Type }}
#endif
#endforeach
</th>
</tr>
#endforeach
#endif

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

nested foreach loop break in laravel

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.

Trying to get property of non-object laravel 5.4

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

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

Resources