In an attempt to learn using events I have followed this tutorial
In my App\Events\ThingWasDone.php i have this
Session::put('testevent', 'it works!');
$author= Entity::find(261);
$author->still_active_URL = 99;
$author->save;
The Session is properly defined and I can get it in my View files.
Any attempt to write to DB produces this error:
LogicException in Model.php line 2632:
Relationship method must return an object of type
Illuminate\Database\Eloquent\Relations\Relation
I tried options like passin or not passing any variable:
\Event::fire(new ThingWasDone($object->id));
\Event::fire(new ThingWasDone());
Didn't help.
Any hint?
change
$author->save;
to:
$author->save();
This might solve the error.
Related
I am doing HTTP testing in Codeigniter 4 and am getting the following error:
CodeIgniter\Exceptions\PageNotFoundException: Controller method is not found
The reason for this is I am trying to call a get() method on a route that only has a post (switching it to add results in a different response). That is correct. I am trying to make sure that the page returns 404 if someone tries to hack it. The problem is Codeigniter doesn't assert Status is 404 it simply falls over. Here is my code triggering:
$r = $this->withSession($session)->get($url);
$r->assertStatus(404);
Note the assert never runs! Instead I get the original error message. Surely I should be able to check for 404 routes (I've tried '404' instead of int) ?? Yes I could set a custom route that allowed Get but I want to test it as-is rather than what it isn't.
How do I get CI4 to accept my incorrect route as a 404?
My solution is to define a constant (IN_TESTING) for when I am testing and then edit system/Codeigniter run() function. This is of course amending the core system which some people won't like but until there is a fix (I am using 4.1.2) then this will suffice.
catch (PageNotFoundException $e)
{
if (!defined('IN_TESTING') || IN_TESTING == false){
$this->display404errors($e);
} else {
$this->router = Services::router($routes, $this->request);
$path = $this->determinePath();
$controller = $this->router->handle($path);
$method = $this->router->methodName();
echo $path."\n".$controller."\n".$method;
exit;
$this->response->setStatusCode(404);
return $this->response;
}
}
Key tip if you do make changes to system files, track them! If you then need to run an upgrade you'll know what changes you've made.
I passed an object in a function in laravel.
I tried to assign the value of a property to a variable. It pops an error.
When I tried to figure out what was going on I tried to dd ()the value it worked.
But when I tried to echo the same it does not.
What am I mising.
On using this,
location1 and location2 are two objects of the location class.
The function is here:
function geodistance($location1,$location2){
dd($location1->lat);
$lat1=$location1->lat;
}
it prints
"28.612072"
But when I change the same function to
function geodistance($location1,$location2){
echo($location1->lat);
$lat1=$location1->lat;
}
The output error is:
Trying to get property 'lat' of non-object
Even the function
function geodistance($location1,$location2){
echo $location1->lat;
$lat1=$location1->lat;
}
gives the same output
The aim to remind you is to assign the value to a variable like so.
function geodistance($location1,$location2){
$lat1=$location1->lat;
}
When I echo $location1 from within the function geodistance() gives out and error which makes sense.
function geodistance($location1,$location2){
echo $location1;
$lat1=$location1->lat;
Object of class App\MyClasses\city could not be converted to string
When I dd ($location1 from the funciton like so it gives the right result.
function geodistance($location1,$location2){
dd($location1);
$lat1=$location1->lat;
like so:
city {#13410 ▼
+id: 2245
+info: "{}"
+name: "New Delhi"
+lat: "28.612072"
+lon: "77.22978"
+timezone: null
+weightedrating: null
+country_id: 1
}
It seems i am missing out something very trivial. :(
As pointed out by #salman zafar and #apokryfos, the mistake was that I was passing an array.
When I was doing a dd, after processing the first data the programme was terminated.
The array that was passed was not completely correct. The last data point in my array was not an object of the same class.
So the data ended up being corrupted when using echo. Echo would print one data after another but eventually it would get a wrong input.
The best way to catch these errors (after so much experimenting) is to write the values of the variable in a text file on each iterartion.
What that does is that it catches the error in your text file and u get to know after how many iterations the programme stopped.
Hope this helps others from making such obviously glaring mistakes which are tough to catch (atleast for me since this was the first of such errors)
Controller Code:
$tokenAmount = json_decode($token->getBody())->result;
return view('account')->with(compact('tokenAmount'));
View Code:
<label>Wallet Balance {{tokenAmount}}</label>
When I run the code. I'm getting following error.
Use of undefined constant tokenAmount - assumed 'tokenAmount'
PS: I tried following method also.
$tokenAmount = json_decode($token->getBody())->result;
return view('account', compact('tokenAmount'));
If I do echo $tokenAmount, it is printing the value without any error.
Please update your view code and check like:
<label>Wallet Balance {{tokenAmount}}</label>
to
<label>Wallet Balance {{$tokenAmount}}</label>
I have the function
index($errorMsg, $successMsg) {....}
It works when I type in the URL.
http://localhost/website/index.php/home/index/1234/5678
But It does not work But when I type in the URL.
http://localhost/website/index.php/home/index//5678
5678 will be $errorMsg.
Is there any hints
Really bad solution for passing success or error parameters via function arguments by get method in CI.
Try use session flash data to pass success or error messages in redirection view.
$this->session->set_flashdata('errorMsg', '1234');
$this->session->set_flashdata('successMsg', '5678');
And show variables:
function index()
{
echo $this->session->flashdata('errorMsg');
echo $this->session->flashdata('successMsg');
}
Use this solution to avoid errors.
Your solution
Declare function like this
index($errorMsg, $successMsg=NULL) {....}
Explanation
index($errorMsg, $successMsg) function required both arguments(variables). If you don't pass it will produce error which is happening in your case.
index($errorMsg, $successMsg=NULL) function required first one and 2nd one is optional.If you don't pass 2nd argument $successMsg value will be null.
Note
/home/index//5678 no need use double slash after index.One will solve your purpose.You need to just check $successMsg.If it is null means you passed only $errorMsg
I spent more than 10 hours to find out the typo for debugging my PHP program. I expected PHP would produce an error when using an undefined variable. But when it is used as an object in a method, it doesn't. Is there a reason for it?
<?php
$oClass = new MyClass;
// $oCless->tihs('key', 'taht'); //<-- triggers the error: Notice: Undefined variable
$oClass->tihs('key', 'taht');
echo $oClass->arr['key'];
class MyClass {
public $arr = array('key' => 'foo');
function tihs($key, $value) {
$tihs->arr[$key] = $value; //<-- this does not cause an error message.
}
}
?>
Normally if the error reporting level is set to E_ALL | E_STRICT (or E_ALL as of PHP 5.4.0) it should spit out an E_STRICT error. For instance, this code:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr = 453;
Produces:
Strict Standards: Creating default object from empty value in [...]
Interestingly enough, if you specifically create an array instead of an ordinary variable as a property of an object that doesn't exist, e.g.:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr[25] = 453;
No strict standards error is shown! It looks like this could potentially be something PHP folks might want to fix, because I'm not aware this is documented nor I think there's a legitimate reason for this behaviour.
For the record, in both cases regardless of the error a new stdClass is being created on the fly instead, like sberry mentions in his answer.
It is because of PHP trickery...
Under the covers, PHP is actually creating an object called tihs, adding an array to the object called arr and setting key to value.
Here is a print_r($tihs); after the assignment:
stdClass Object
(
[arr] => Array
(
[key] => taht
)
)
You seemed to have misspelt $oClass as $oCless
Agreed, $oCless instead of $oClass would give you an undefined variable error.
Also, "this" is a keyword in most languages and may be in php as well. You should refrain from using it so that it doesn't come out in other languages as a habit. You'll get way more errors if you're using "this" as function and variable names. You wouldn't even get things to compile.