This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
My goal is set value if field is empty.
But get "Trying to get property of non-object" error.
last_day is empty field, how to assign value 1 ?
public function getPointsForExercises(){
$ls = Auth::user()->lessons()->get();
$last_day = Auth::user()->user_settings->last_day;
if (empty($user->user_settings->last_lesson)) {
$user->user_settings->last_lesson = 1;
}
First problem: $user variable is not defined.
What you are trying to achieve can be done using the exists function to make sure the relation exists, and then update the value using the update function, like this:
public function getPointsForExercises() {
$ls = Auth::user()->lessons()->get();
$last_day = Auth::user()->user_settings->last_day;
if (Auth::user()->user_settings()->exists() && empty(Auth::user()->user_settings->last_lesson)) {
Auth::user()->user_settings()->update([
'last_lesson' => 1
]);
}
The code above is not ending the function nor is it using the variables $ls and $last_day as your code.
You did not set the $user object.
firstly you have to make object :
$user = Auth::user()
Related
This question already has an answer here:
Message: Trying to access array offset on value of type null [duplicate]
(1 answer)
Closed 4 months ago.
This is my code:
$evaluationjob = evaluation_elements_jobs::where('job_id', $user->job_id)
->where('company_id',$company_check->id)
->first();
The error in this line:
$items = json_decode($evaluationjob["element_degree"]);
The error message is:
Trying to access array offset on value of type null
You can try this:
$evaluationjob = evaluation_elements_jobs::where('job_id', $user->job_id)
->where('company_id',$company_check->id)
->first();
if ($evaluationjob != null && isset($evaluationjob["element_degree"])) {
$items = json_decode($evaluationjob["element_degree"]);
}
This will check if $evaluationjob is not null and if the value $evaluationjob["element_degree"] exists in the variable.
The error you are having here is the fact that you're trying to access a property on a variable that is null;
So you need to put a check on the variable and the property.
if ($evaluationjob != null && isset($evaluationjob["element_degree"])) {
$items = json_decode($evaluationjob["element_degree"]);
}
This question already has an answer here:
How to get the string name of the argument's type hint?
(1 answer)
Closed 2 years ago.
I have method which cast array to object by using
$class = get_class($object);
$methodList = get_class_methods($class);
But now I need had information about expected type of variable too. For example from this method:
public function setFoo(int $foo)
{
}
I need get int too. There is any option to get it?
You can use Reflection. Specifically ReflectionParameter::getType().
function someFunction(int $param, $param2) {}
$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();
assert($reflectionType1 instanceof ReflectionNamedType);
echo $reflectionType1->getName(), PHP_EOL;
var_dump($reflectionType2);
The above example will output:
int
NULL
This question already has answers here:
How to pass a variable inside a function?
(1 answer)
How to access array element inside another array [duplicate]
(1 answer)
Closed 5 years ago.
I have an API's Function :
$transaction=$tran[4];
function coinpayments_api_call($cmd, $req = array(),$transaction) {
curl_init($transaction);
}
$transaction variable not passing into function coinpayments_api_call.
function not taking values from out side.
I also make $transaction varible GLOBAL ,but still same problem ,
Please Help
Make your code like this
$transaction=$tran[4];
function coinpayments_api_call($transaction,$cmd, $req = array(),$txnid) {
curl_init($transaction);
}
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
I am trying to write a PHP function to write error messages to an array. Not sure what I'm doing wrong, still trying to get to grips with functions.
I can make it work without functions, so I guess its the way I'm writing the function that is wrong.
function writeerrors($arr_key, $arr_val){
$errors[$arr_key] = $arr_val;
return;
}
Then I call it here when I check if the form field is empty. If it is empty I want it to write to the $errors array.
//check if empty
if(empty($fname)){
//write to error array
writeerrors('fname', 'Empty field - error');
//Flag
$errors_detected = true;
}else {
Do something else ..}
This is the form... (ONLY TRYING TO VALIDATE FIRST NAME FIELD FOR NOW):
http://titan.dcs.bbk.ac.uk/~mgreen21/p1_prac/PHP_BBK/P1/hoe9/index.php
You just need to specify the global $errors variable, which you have created outside of your function.
function writeerrors($arr_key, $arr_val){
global $errors;
$errors[$arr_key] = $arr_val;
return;
}
This question already has answers here:
Passing an optional parameter in PHP Function [duplicate]
(6 answers)
Closed 7 years ago.
Okay - I was able to the pass a string assigned to the variable $myfile into the function. If it could be better, please provide feedback- learning PHP.
<?php
$myfile = $row_rs_recordview['headstone'];
echo "$myfile"; // verify the file name
function readGPSinfoEXIF($myfile)
{
global $myfile;
$exif= exif_read_data("headstone/".$myfile, 0, true); //
if(!$exif || $exif['GPS']['GPSLatitude'] == '') //Determines if the
//geolocation data exists in the EXIF data
{
return false; //no GPS Data found
echo "No GPS DATA in EXIF METADATA";
}
?>
Insights appreciated!
Thanks
you can't access the global variables inside function directly.
//add global before variable declaration like this
function fun(){
global $row_rsUpdate['headstone'];
}