laravel Blade is their a neat solution to : Trying to get property of non-object - laravel-5

I have thins line of code
{{($user->company_privileges->level < 3 ? '' : ' disabled') }}
In my menu file
but when a user doesn't have a company I get the error
Trying to get property of non-object
Is their a neat way of dealing with this in one line?
Something like this comes to mind
{{ ($user->company_privileges === null) : '' ? ($user->company_privileges->level < 3 ? '' : ' disabled') ) }}
as the second if statement is only ran if the user has a company
I have the statement inside a tag aswell
<a class="{{($user->company_privileges->level < 3 ? '' : ' disabled') }}">

Why not something like
<a class="{{($user->company_privileges && $user->company_privileges->level < 3 ? '' : ' disabled') }}">
Essentially, what you need here is to know that null == false for bool comparison. Check out the PHP type comparisons table

Related

Laravel having space output issue in blade file

I have Laravel code in Blade like this :
<span id="pk_dens" name="pk_dens" class="text-#if($productpages->pk_dens > 2.2 && $productpages->pk_dens < 3.3) success #else danger #endif">
{{$productpages->pk_dens}}%
</span>
So in class="text-#if($productpages->pk_dens > 2.2 && $productpages->pk_dens < 3.3) success #else danger #endif"
it will add space in class like text- danger and text- success so class not apply
so how can I avoid that spce in if else condition ?
Use ternary operator
<span id="pk_dens" name="pk_dens" class="text-{{ ($productpages->pk_dens > 2.2 && $productpages->pk_dens < 3.3) ? 'success' : 'danger' }}">
{{$productpages->pk_dens}}%
</span>
https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
I think you are missing here is ? in ternary syntax and to remove space just remove all white spaces from the span tag and make it in just one line.
<span id="pk_dens" name="pk_dens" class="text-{{ ($productpages->pk_dens > 2.2 && $productpages->pk_dens < 3.3) ? 'success' : 'danger' }}">{{$productpages->pk_dens}}%</span>

Using if condition to display data in Blade

How print value in this code:
<li class="price-val">{{ $value->price ? '$value->price € month<span class="price-fea-ct">*VAT Included</span>':'$value->price € month' }}</li>
$value->price variable is have 0 & 9 value respectively.but it does print value.print $value->price just.
Something like this should work
<li class="price-val">
#if ($value->price)
{{{ $value->price }}} € month<span class="price-fea-ct">*VAT Included</span>
#else
{{{ $value->price }}} € month
#endif
</li>
However for me the condition doesn't make sense - if price is 0 you will display 0 / month and it does not seem to be logical
You have syntax errors, try this:
<li class="price-val">
{{
$value->price ?
$value->price . '€ month <span class="price-fea-ct">*VAT Included</span>' :
$value->price . '€ month'
}}
</li>

Debug Undefined index ban end php

I have 2 debug notices both this message below
[phpBB Debug] PHP Notice: in file [ROOT]/memberlist.php on line 428: Undefined index: ban_end
the line in question is
if ($member['ban_end'] >= time() || $member['ban_end'] == '0')
now if i add isset like this it kills 1 debug
if (isset($member['ban_end']) >= time() || $member['ban_end'] == '0')
how do i apply isset so both ban_end gets covered by isset
The code you post won't work since it's comparing the value of time() against a boolean.
You could do this:
if (isset($member['ban_end'])) {
if ($member['ban_end']) >= time() || $member['ban_end'] == '0') {
// some code
}
} else {
// do something if variable not set
}
You probably need to understand why the variable isn't set though, rather than applying a band-aid fix.

How to generate tuple in ? operator of pig

My code is as follows
temp = foreach requiredData generate (recordType == 3 ? controllingCalledNum : callingPtyNum)as ServiceNumber, (recordType == 3 ? callingPtyNum : controllingCalledNum)as DestinationNumber;
Here my code is reduntant..
Can I generate tuple inside '?' operator and do something like this which I can further FLATTERN
temp = foreach requiredData generate (recordType == 3 ? (controllingCalledNum,callingPtyNum) : (callingPtyNum,controllingCalledNum))as (ServiceNumber,DestinationNumber);
I am getting error if I try to do like this
Please help me.
Use the built-in TOTUPLE UDF:
temp = foreach requiredData generate FLATTEN(recordType == 3 ? TOTUPLE(controllingCalledNum,callingPtyNum) : TOTUPLE(callingPtyNum,controllingCalledNum))as (ServiceNumber,DestinationNumber);

Linq to nhibernate sql 1 = 1 equivalent

I am trying to do a search with LINQ to NHibernate.
I have this code:
from d in rep.QueryAll<Document>()
where
d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita
&& (d.NrPolita.Contains(query) ||
d.Contractant.CodUnic.Contains(query) ||
d.Contractant.Denumire.Contains(query) ||
d.Plata.IdTranzactie.Contains(query)) &&
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
select new
{
The problem is that I have some select inputs that have general values. Something like this:
<select id="tippolita" >
<option value = "-1">Any value</option>
<option value = "1">Value 1</option>
<option value = "2">Value 2</option>
<option value = "3">Value 3</option>
</select>
So when "Any value" is selected the where statement should be true like I wrote here:
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
This is almost the same as what I would write in SQL.
An error occurs inside the Nhibernate source code at line 33 in the file "Linq\NHLinqExpression.cs"
_expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);
This error actually comes from the re-linq library.
One obvious workaround is to just write 3 if statements and put the appropriate LINQ queries in each of them but that means writing a lot more code.
Is there any way to make this kind of query work without copy-pasting the entire query and modifying just a little of it?
P.S.
This is the inner exception:
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=Anonymously Hosted DynamicMethods Assembly
StackTrace:
at lambda_method(Closure
)
I would rewrite this:
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
as
(TipPolita == null || d.Tip == (TipProdus)TipPolita) &&
(StareDocument == null || d.Stare == (StareDocument)StareDocument)
I don't know whether it'll work in NHibernate or not, but it's more idiomatic C# at least, so I would expect that it's more likely to be supported.
As an alternative, you could just replace "1 == 1" with "true".
Well, figured out how to do this the proper way
var date = rep.QueryAll<Document>().Where(d => d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita);
if (!string.IsNullOrEmpty(query))
date = date.Where(d => (d.NrPolita.Contains(query) ||
d.Contractant.CodUnic.Contains(query) ||
d.Contractant.Denumire.Contains(query)));
I just move the ifs into the code and build the query(or rather the IQueryable) bit by bit

Resources