Bibash Adhikari
Bibash Adhikari

Reputation: 173

Object of class Closure could not be converted to string

I have got this error while using the code below in DetailView of Yii 2.

Object of class Closure could not be converted to string

The code is:

[
    'format' => 'raw',
    'attribute' => 'title',
    'value' => function($model1, $key) {
        if ($model1->book->language == 1) {
            $m = "<p class='n'>" . $model1->book->title . "</p>";
        } else {
            $m = $model1->book->title;
        }
        return $m;
    },
    'contentOptions' => ['class' => 'text-center'],
    'headerOptions' => ['class' => 'text-center']
],

Can you guys help me?

Upvotes: 2

Views: 1738

Answers (1)

Bizley
Bizley

Reputation: 18021

DetailView does not take closure like GridView for value, just string. Change it to:

'value' => $model1->book->language == 1 
           ? "<p class='n'>" . $model1->book->title . "</p>" 
           : $model1->book->title,

Upvotes: 3

Related Questions