Object of class Carbon\Carbon could not be converted to int LARAVEL - laravel-5

How do I implement if the schedule has been expired where in I want to return a status of "Match will start soon".
here is the code:
#if($match->schedule > 0)
<strong id="match_schedule">Match will start soon</strong>
#endif
I tried :
#if($match->schedule > $match->schedule)
<strong id="match_schedule">Match will start soon</strong>
#endif
but it doesn't work. any ideas?

It seems you are trying to compare a Carbon instance to int 0. This causes the exception:
Object of class Carbon\Carbon could not be converted to int.
You can check if the schedule lies in the past by comparing it like this:
#if($match->schedule < Carbon\Carbon::now())
<strong id="match_schedule">Match will start soon</strong>
#endif

Related

I got this error on laravel blade file -ErrorException A non well formed numeric value encountered on helper funtion

Form helper get string value. Then convert numeric value. Other helper functions work properly but only this portion doesn't work.
if(isset($result[$product->id])){
$productArray[$product->id]+= #helper::getCtnQty($product->id,$result[$product->id]); // Error get from this line. but when dd I get value in int form
$singleProductArray[$product->id] = $productArray[$product->id];
<td style="text-align:center;font-weight: 700">
{{#helper::getCtnQty($product->id,$result[$product->id])}}
</td>
}
I needed integer value for code but I get string. And This error usually pops up when we try to add an integer with a string or some type of non numeric field.
So, firstly I check type by gettype(). Then write (int) before helper function.
if(isset($result[$product->id])){
$productArray[$product->id]+= (int)#helper::getCtnQty($product->id,$result[$product->id]);
$singleProductArray[$product->id] = $productArray[$product->id];
<td style="text-align:center;font-weight: 700">
{{#helper::getCtnQty($product->id,$result[$product->id])}}
</td>
}

How to get the value of an attribute in cypress

I have this HTML element, and I am trying to get the value of for attribute
<div class="test">
<label for="aboqo_46" data-test = "user-test" >
I want to get retrieve the value in for which is aboqo_46 in the above code. How can this be achieved?
I have tried the following but could not get the values.
const result = cy
.get('[data-test="user-test"]')
.invoke('attr','for')
cy.log(result)
The above code logs the result value as Object{5}
and
cy
.get('[data-test="user-test"]')
.its('for')
Your code is a little off, it's not returning the attribute value it's returning a Chainer object so that Cypress can chain commands.
This will work:
cy.get('[data-test="user-test"]')
.invoke('attr','for')
.then(value => cy.log(value))

Thymeleaf, get current date and subtract x amount of days

I am trying to implement an if statement on my Thymeleaf template that will change the colour of a value based on the current time (minus a specific amount of days).
Now from my understanding there are three ways to declare a date in Thymeleaf:
//For the new LocalDateTime, LocalDate classes
#temporals.createNow()
//For an instance of java.util.Date
#dates.createNow()
//For an instance of java.util.Calendars
#calendars.createNow()
Now my model uses instances of java.util.LocalDate so I tried tackling the problems in two different ways (unsuccessfully).
The first thing that come into my mind was to implement the following:
td th:if="${user.expiry_date.isBefore(#temporals.createNow().minus(7, ChronoUnit.DAYS))}"
th:text="${#dates.format(user.expiry_date, 'dd-MM-yyyy')}"style="color: red"/>
But I get the following SpelEvaluationException:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'DAYS' cannot be found on null
The other approach would be to subtract the date on the server and pass the value through a variable:
//method in the #Controller class
model.addAttribute("userList", organisationService.getAllOrganisations());
**model.addAttribute("localDateNow", LocalDate.now().minusDays(7));**
But even then, accessing another variable inside the same spel expression seems impossible, or at least all my attempts failed:
<td th:if="${user.expiry_date.isBefore(localDateNow)}"
th:text="${#dates.format(user.expiry_date, 'dd-MM-yyyy')}"style="color: orange"/>
and:
<td th:if="${user.expiry_date.isBefore(${localDateNow})}"
th:text="${#dates.format(user.expiry_date, 'dd-MM-yyyy')}"style="color: orange"/>
For the first case, you can't directly call classes by name I believe. you'd have to do "${user.expiry_date.isBefore(#temporals.createNow().minus(7, T(java.time.temporal.ChronoUnit).DAYS))}"
And for the second I think you should be using ${#temporals.format()} as you'r using LocalDateTime not the clasic Date object but as theres no stacktrace given this is just a guess

Get the last element of an array within a struct in a Golang template [duplicate]

This question already has answers here:
How do I get the last element of a slice in a golang template
(3 answers)
Closed 1 year ago.
I'm building a simple forum in Go for a school project, and I'm passing a data struct to a template to display all the posts in a subforum. The data I'm passing to the template is this:
type Data struct {
ID int // ID of the subforum
User User // logged-in user
Posts []Post // all the posts of the subforum
}
The Post struct within the Data struct is like this:
type Post struct {
ID int
Title string
Content string
Date time.Time
[...]
Author User
Comments []Comment
}
And the Comment struct is similar to the Post struct. When I display the list of all the posts, I would like to also show the number of replies and the date/time of the last reply.
In my HTML template, I can get the number of replies like this:
{{range .Posts}}
<p>Replies: {{ len .Comments }}</p>
{{ end }}
...but I can't seem to get my head around getting the date of the last element of the Comments array. I know you can get the first element with the index keyword and the value '0', but I can't use (len .Comments -1) inside the template to get the last element as '-' is a forbidden character. I'll probably just make a second function to get my comments sorted by descending order from my SQLite database, but I was wondering if there was a simple way to work with the indexes in Go templates.
Thank you.
There's not a clean way to do this with Go templates, however this is a workaround here. A simpler workaround would be to add the last item to your struct before passing the struct to the templater. What you're doing is moving the complicated logic out of the template (templates weren't designed to do this anyways) and into the Go code.
type Post struct {
....
Comments []Comment
LastComment Comment
}
Then in your template, just do
{{ .LastComment }}
You can use a custom function inside your template to get the last element:
fmap := template.FuncMap{
"lastElem": func(comments []Comment) Comment {
return comments[len(comments)-1]
},
}
tmpl, err := template.New("tmpl").Funcs(fmap).Parse(tpl)
And then use it in your template as:
{{range .Posts}}
<p>Replies: {{ lastElem .Comments }}</p>
{{ end }}

How to hide message on the remaining pages in laravel?

I have the following including of templade blade in main layout:
#if (Auth()->user()->verified == "0")
#include('common.verify-error')
#endif
So, how to display #include('common.verify-error') on the all URL excluding /test, /out
Should I do this in controller or simple if condition in template?
You can use is() method with mask:
#if (auth()->user()->verified == 0 && !request()->is('test/*') && !request()->is('out/*'))
#include('common.verify-error')
#endif
To start, there's probably a few different approaches to this. Personally, I'd do something along the lines of a middleware class...or inject a class in the view, etc.
However, he's another solution that may suit your needs better.
EDIT: Made a change. I'm assuming you use named routes (and should, in the event you want to change urls, etc. You won't have to deal with changing all these if statements for example).
#if (Auth()->user()->verified == "0" && !Route::is('test') && !Route::is('out'))
#include('common.verify-error')
#endif

Resources