2

Here is my code:

$tracking_codes_obj = new TrackingCode;
$tracking_code = 140;

if ( !is_null($tracking_code) )
    $tracking_codes_obj->where('tracking_code', $tracking_code);

$tracking_codes = $tracking_codes_obj->orderBy('expired')->orderBy('id')->paginate(10);

As you can see $tracking_code is not null, so that condition is true and that where clause should be applied. But still the result is the same as when I completely remove that condition.

Why and how can I fix it?

2 Answers 2

3

The correct syntax is:

$tracking_codes_obj = $tracking_codes_obj->where('tracking_code', $tracking_code);
Sign up to request clarification or add additional context in comments.

1 Comment

Also one PHP question, when exactly should I initialize an object into a variable and when shouldn't? For example in a database connection, no need to initialize anything and everything will be applied by using ->. i.e $db_con = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $db_con->exec("set names utf8");
1

you need to replace if condition like this :

if ($tracking_code != null)

Instead of

if ( !is_null($tracking_code) )

and code will be like this

$tracking_codes_obj = new TrackingCode;
$tracking_code = 140;

if ($tracking_code != null)
{
    $tracking_codes_obj = $tracking_codes_obj->where('tracking_code', $tracking_code);
}

$tracking_codes = $tracking_codes_obj->orderBy('expired')->orderBy('id')->paginate(10);

1 Comment

I'm not sure why did you say I shouldn't have used is_null() function, but anyway your code would work as well because of initializing the object. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.