jeudi 30 juin 2016

Calling eloquent relation multiple times doesn't return data

I'm having this strange behavior in Laravel 5.1 where when I call the relation of an eloquent model more than once within the same code execution, then the second time it doesn't have the data.

class Items extends Eloquent {
    public $table = 'items'

    public function subItems() {
        return $this->hasMany(Item::class, 'items_id');
    }
}

class Item extends Eloquent {
    public $table = 'items_item'
    public $fillable = ['items_id'];
}


$items = Items::create();
Item::create([
    'items_id' => $items->id,
]);
Item::create([
    'items_id' => $items->id,
]);


// works
$first = $items->subItems;
// no data
$second = $items->subItems;
// works
$third = $items->subItems()->get();

Is this normal behavior? Do i have to somehow reset something before calling the relation again?



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

Aucun commentaire:

Enregistrer un commentaire