vendredi 17 juin 2016

Call to a member function getmetavalue() on null

I got an error when a database table is empty when my model's function calls it. This is my code in my AccountMeta model.

public function scopeGetmetavalue($query, $key){
    $data = $query->where('meta_key', '=', $key)->pluck('meta_value');
    if($data->count()){
        if(isset($data[0])){
            return $data[0];
        }
    }

    return " ";
}

This is on my views to call it:

$GroupFile = Auth::user()->account->meta->getmetavalue('file_group_contact');

My Database structure is this:

Schema::create('account_metas', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('account_id');
            $table->string('meta_key');
            $table->string('meta_value');
            $table->timestamps();
        });

There is no issue when there is data in the account_metas table but when it is empty I got the error. any idea of it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire