dimanche 1 mai 2016

Trying to get property of non-object in Laravel unserializing array from database

I have the following class in my code.

class ReportField extends Model
{
    //
    public $name;
    public $quantity;
    public $unitCost;
    public $totalCost;
    public $estimation;
}

I write an array of these objects to a database using serialization.

$fieldsList=array();
foreach($items as $item){
        if($item->sale_type == 1){

                $reportField=new ReportField();
                $reportField->name=$item->item_name;
                $reportField->unitCost=$item->unit_cost;
                array_push($fieldsList,serialize($reportField));
         }
}

$gpForecast=new GPForecast();
$gpForecast->project_id=$project_id;
$gpForecast->fieldList=(serialize($fieldsList));
$gpForecast->save();

After unserialization now i want to convert those objects in array to RecordField objects.

$gpForecast=GPForecast::all()->first(); //here i read one of array i write to database
$fieldList=unserialize($gpForecast->fieldList );
foreach($fieldList as $field){
       echo $field->name;
}

But then I get the error Trying to get property of non-object So how can I correct this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire