vendredi 25 septembre 2015

Laravel Checking if a Dynamic property is set

I have a class foo that belongs to bar.

within foo I have:

public function bar(){
    return $this->belongsTo('App\Models\Bar');
}

I have an object $foo who's bar_id is null.

Within Foo, I have an if statment:

if ($this->bar->id == 1){ echo "in if"; }

Not surprisingly I was getting a Trying to get property of non-object error. So I added a call to isset. But now the code inside the if is never being evaluated because the if is always false. My new if looks like:

if(isset($this->bar) && $this->bar->id == 1){ echo "in if"; }

After setting the bar_id I am still not going into the if. When I try to print bar_id I see the value is not null. Why am I not going into this if?



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

Aucun commentaire:

Enregistrer un commentaire