I am trying to create an instance of a class "announcement".
For example:
$announcement = new announcement;
//code to bind data
$announcement->store();
My store function runs a query to store the information to the database. Everything is working as intended and storing to my database. However when I say:
if($announcement->store())
{
echo 'success';
}
else{
echo 'failure';
}
I get the 'failure' message. Why is my method returning false when it is actually working? Am I missing something with conditional statements and methods?
UPDATE
My store function was not returning anything. So even though it worked it was returning a null value. I return true on query success and now it is working as intended. Thank you.
The store function should return true then your statement will print success message
class announcement {
public function store() {
// after execution code the return value should be like this
return true;
}
}
Related
In my codeigniter application I have specified a session as below
$this->session->isLoggedIn = true;
echo "Session";
echo $this->session->isLoggedIn;
even though I assign true the output shows as 1 instead. What have I done wrong here?
would really appreciate some help
EDIT :- The problem was actually in the following
When I try to acess this session from another method the session value is set to null. My other function is defined as
public function isLoggedIn()
{
var_dump($this->session->isLoggedIn);
if ((isset($this->session->isLoggedIn)=="true"))
{
return "true";
}
else
{
return "Sorry,false";
}
}
the vardump that I am printing in this isLoggedIn function is Null even though the session variable was assigned as displayed in the initial code above
Is it true that in laravel api the first return will break the function and won't let it continue executing the rest of the function?
I have this function
public function get(Request $request)
{
$users = User::doesntHave('reservations')->get();
if ($users) {
return Response::json($users);
}
// do something here if no users attached with reservation and do
// return Response::json($users);
}
when I do this I get no data! What is my mistake ?
I guess what you want is to check if result is empty or not:
if (!$users->isEmpty()) {
return Response::json($users);
}
return Response::json('There is no result');
And yes, you can return response only once.
This is not exclusive to Laravel, but yes. A return statement per definition will return to the caller of the function immediately, so nothing else after it will be executed.
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.
The Overview
I've been experimenting some features which I've learn't using PHP, last night I was working on anonymous functions and for some strange reason when I var_dumped the function it kept returning null.
The Code
Below is the code I've written.
The findOrFail function,
public static function findOrFail($iD, $successCallback = null, $failCallback = null)
{
$db = new Database();
$db->select("users")->fields(["*"])->where(["id" => $iD])->execute("select");
if ($db->rowCount() == 1) {
if (is_callable($successCallback)) {
return $successCallback();
} else {
return true;
}
} else {
if (is_callable($failCallback)) {
return $failCallback($iD);
} else {
return false;
}
}
}
In test.php,
require_once "config.php";
var_dump(User::findOrFail(1, function () {
echo "Found.";
}, function ($iD) {
echo "Failed.";
}));
The Output
The ID 1 exsits so I expect the see when dumping string and the contents to be "Found." however I see this:
Found.NULL
What I have tried?
I looked at another question related to this issue and it said
that it was because of a buggy PHP version (5.3?). So I checked my
PHP version and it is 5.5.8.
I thought maybe because the default parameters ($successCallback and $failCallback) are set to equal null that that may be causing the error to occur. However some quick changes to the code (to remove the null) showed that it didn't fix anything.
So my question is, Why is it showing null? If anyone could shed some light on this issue it would be much appreciated.
Your anonymous functions don't return anything, they just call echo to print something. Use:
return "Found";
and
return "Failed";
Part of my application is a multi-stage checkout process; during the latter pages of this I first run a sanity check on each request to verify the user actually has some items in their basket: if not they're sent back to the beginning.
I have a controller function like this which is called from multiple routes for DRY purposes.
private function checkBasketFull($request)
{
if (self::isBasketEmpty($request)) {
return redirect('/')->with('status', config('app.empty_basket_message'));
}
}
When I call it, I can't just do:
self::checkBasketFull($request);
because without a return the redirect doesn't fire, only the session data is sent.
And I can't do:
return self::checkBasketFull($request);
because that will give an error if there's no redirect or abort the method if checkBasketFull returns anything else.
My current (working) code is:
$check = self::checkBasketFull($request);
if ($check) {
return $check;
}
Is there an alternative way of writing this on a single line, or modifying the checkBasketFull function, so the redirect will occur if the basket is empty but execution will continue as normal if it isn't?
Either use this:
if ($redirect = self::checkBasketFull($request)) return $redirect;
Or throw an error and catch it in the global error handler.
However, instead of returning and checking that for a redirect like that, I'd much rather keep it as two completely separate methods:
public function someRoute(Request $request)
{
if ($this->isBasketEmpty($request)) return $this->redirectBasketEmpty();
// Continue processing this request...
}
protected function isBasketEmpty(request)
{
// run your login here...
}
protected function redirectBasketEmpty()
{
return redirect('/')->with('status', config('app.empty_basket_message'));
}
Feels cleaner to me.
I'm relatively new to CodeIgniter and this may be an obvious answer, but I am unable to find anything in the documentation.
I know how to use form_validation->run() to validate a form and run some code when it is submitted successfully. What if I wanted to do something with a form that did not require any sort of validation? Is there a form->run() equivalent that returns true when a user submits the form? Something like:
page.php
public function update($id)
{
$this->load->helper('form');
if($this->form->run())
{
// do some stuff after user has submitted form
}
}
I don't think there exists such method, but you can do it manually
For example:
if ( ! isset($_POST['something'])) // OR $this->input->post('something');
{
return false //maybe?
}
else
{
//$something = $_POST['something'];
return true //maybe?
}
A statement like this should work. You just need to check if any post data exists.
if ($_POST)
{
// do something
}