How I can check existing property $this->team->playerAssignment->player in more rational way? Now I check it like this:
if ($this->team)
if (($this->team->playerAssignment))
if (($this->team->playerAssignment->player))
Try isset php function.
isset — Determine if a variable is set and is not NULL
if(isset($this->team->playerAssignment->player)){
}
The following has always works best for me:
if(isset(Auth::user()->client->id)){
$clientId = Auth::user()->client->id;
}
else{
dump('Nothing there...');
}
The best way is
if (
isset($this->team)
&& isset($this->team->playerAssignment)
&& isset($this->team->playerAssignment->player)
){
// your code here...
}
Because PHP will stops if the first is to false, if first object exists, it continue to second, and third condition...
Why not use only && $this->team->playerAssignment->player ?! because if player has 0 for value it will be understood as a false but variable exists !
You can easily check by null coalescing operator
if ($this->team->playerAssignment->player ?? null)
Related
is it possible when submitting a form that redirects me with a variable to check if that variable is null? Because when i have if($variable === null) in my code and i dont pass the mentioned variable when loading the page it will result in error that $variable is not defined
you need to define the variable before put it in if condition
not in the if
update:
use this in return : return view('name_page')->with('variable',$variable);
the exception told you that variable doesn't exist on memory so you to check if it exists not in it's value for that you have change your if condition from
if($variable === null) to be if(isset($variable))
I'm trying to create a function, it exists and does not exist in the search feature. Condition does not exist, will display Error 404. Condition, exists. Then it will display from search. But in the code I wrote, it only shows Error 404.
This is my model:
function search_album($keyword){
$result=$this->db->query("SELECT tbl_album.*,DATE_FORMAT(album_date,'%d %M %Y') AS date FROM tbl_album WHERE album_title LIKE '%$keyword%'");
return $result;
}
This is my controller:
function search(){
$keyword=str_replace("'", "", $this->input->post('xfilter',TRUE));
$x['data']=$this->model_album->search_album($keyword);
if(empty($x) && empty($keyword)){
$this->load->view('view_contents',$x);
}
else if (!empty($x) && !empty($keyword)){
$this->load->view('view_error_404');
}
}
I've tried from this source, but it doesn't work. Can you help me?
There are several errors in your code:
ONE: You have interchanged your if statements. Like, if the search did not return any data and the keyword was not supplied, it is supposed to display the "view_error_404", otherwise, it is supposed to load data into the view "view_contents".You did the vice versa I have corrected that for you in the code below.
TWO: You are checking if $x is empty which will never be empty as you have initialized $x['data']. Note that the model can return empty data, such that $x['data'] is empty. Instead, check if the search result is empty by replacing empty($x) with empty($x['data'])
THREE: In your model, you are returning the query builder class, but not the actual data from the query statement. instead, replace return $result; with return $result->result();
FOUR:
From your if statements, you need to add an else statement so that if the 2 conditions are never met, it can execute. With your current implementation, there is a state which will not meet first or second conditions and will lead to a blank screen.
if(empty($x['data']) && empty($keyword)){
$this->load->view('view_error_404');
}else if (!empty($x['data']) && !empty($keyword)){
$this->load->view('view_contents',$x);
}else{
$this->load->view('view_error_404'); // replace this code with your preference
}
you need to mention search function in routes.php file like this:-
$routes['search'] ='your_controller_name/search';
without knowing your function model_album->search_album($keyword) and assuming it returns a string or array, you don't need to worry about checking for the keyword, since without a keyword the function should return false or empty.
anyway you mixed up your if/else conditions, since you are checking for not empty results to return a 404, instead of empty result returning the 404.
this should do it:
if(empty($x)){
$this->load->view('view_error_404');
}
else {
$this->load->view('view_contents',$x);
}
How do I check if a SASS Map already exists and only define one if this is not the case?
I have tried:
#if ($myMap) {
// do someting
}
and
#if variable-exists($myMap) {
// do something
}
But I get the error "Undefined variable: "$myMap"?
I'm sure this is pretty straghtforward but I can't seem to find the answer online?
Thanks
It's a little confusing, but when checking for a variable's existence, skip the $. You also need to set it as a global variable so it doesn't get scoped only to the #if block. This works:
#if variable-exists(myMap) == false {
$myMap: (
1: "foo",
2: "bar"
) !global;
}
// ... now you can use your variable
You can set the map to exist as null be default, which means that if it isn't explicitly set anywhere, then the variable still exists but with a null value. You can then check if the value of the variable is null using an if statement.
$myMap: null !default;
#if $myMap == null {
}
my code:
$atts->each(function($row){
if($row->key == 30){
$flag = $row->value;
echo $flag;
}
});
The value of $flag will be printed to the site. However, when I try to use the variable $flag outside the loop the variable is unknown.
Can someone tell me what I am missing or what has to be done to have access to that variable?
Thanks in advance!
Regards,
Andreas
You would need to define that variable in the current scope and import that variable into your callback's scope with use.
$flag = null;
//Access flag by reference
$atts->each(function($row) use (&$flag) {
...
});
Since we're using Laravel Collections, I suggest you not use each() for this.
if ($row = $atts->where('key', 30)->first()) {
$flag = $row->value;
}
This assumes there is only one row with a key of 30 though.
Here is the application.properties file:
myVar=${SOME_VAR:#{null}}
result=myVar is #{myVar != null && myVar.length() > 0 ? '' : 'not'} populated
What I am trying to get is if the environment variable SOME_VAR is set (and not blank), the property result should be myVar is populated, otherwise myVar is not populated.
The code I put above doesn't work (the line to set result), and I have also tried different combinations of #{} and ${}, including wrapping myVar, but no success so far.
What is the correct way to do? Thanks.
You wont be able to refer the myVar field directly if your member variable are private. So you should put your condition directly on the property value.
please check below expression as per your requirement.
#Value("myVar is #{ '${SOME_VAR}' != null && '${SOME_VAR}'.trim().length() > 0 ? '' : 'not'} populated")
private String result;
The #{ } is an expression language feature, while ${ } is a simple property placeholder syntax.
I ended up doing
result=myVar is #{'${SOME_VAR:#{null}}' != '#{null}' && '${SOME_VAR:#{null}}'.trim().length() > 0 ? '' : 'not'} populated