jeudi 16 juin 2016

Laravel 5.1, getting the collection object in the constructor

can anyone explain why this happens? I can't get the $property collection inside my constructor. But via the getProperty method, it works fine.

Yes, the property collection does exist and is passed fine.

<?php

namespace App\Library\Repositories;

use App\Property;

class Match implements MatchInterface
{

    public $property;

    private $personalityTagValuation;



    public function __construct(Property $property) {
        $this->property = $property;

        $this->personalityTagValuation = $this->CalculatePersonalityTagValuation();
    }



    private function CalculatePersonalityTagValuation() {
        dd($this->property->id); // returns null
    }

    public function getProperty() {
        dd($this->property->id); // returns 26
    }
}

This is how I instantiate the class:

$property = Property::findOrFail(26);
$matches = new Match($property);
$matches->getProperty();

If I outcomment the dd in CalculatePersonalityTagValuation() so it passes on to getProperty, I get the property collection as expected. But I don't get it within the constructor. Also, if I do a dd($property->id) in the constructor, I also get null.

Also $matches->property fetches the property finely.

Why is that?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire