mardi 21 février 2017

Variable prints both Null and Not Null in Laravel

PHP Test

$username = 'matt';
scope($username); //pass matt to scope

function scope($username = null) {

    // Not null
    if (!is_null($username)) {
        print $username;
    }
    // Is null
    else if (is_null($username)) {
        print 'null';
    }
}

Works correctly. It prints matt, since that variable was passed.

Laravel

Using Scopes with Laravel 5

Component

public function init() {
    $username = $this->param('username'); //matt
    $this->applyUser($username); //pass to scope function
}

Model

public function scopeApplyUser($query, $username = null) {

    // Not null
    if (!is_null($username)) {
        print $username;
    }
    // Is null
    else if (is_null($username)) {
        print 'null';
    }

    return $query->where('username', $username);

}

It prints both matt and null at the same time.

It returns query results only null.

How is this possible?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2l5XCqF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire