php - Trying to access array offset on value of type bool in Yii2
Get the solution ↓↓↓i dont know how to solve this problem
here is my controller
public function actionView($id)
{
$view = (new \yii\db\Query())
->select(['blog.id', 'user_id', 'body', 'title'])
->from('blog')
->join('INNER JOIN', 'users', 'blog.user_id = users.id')
->where(['blog.id' => $id])
->one();
return $this->render('view', compact('view','id'));
}
here is my view
<div class="col-lg-4">
<h2><?= $view['name'] . ' ' . $view['surname'] ?></h2>
<h3><?= $view['title']; ?></h3>
<p><?= $view['body']; ?></p>
<a href="<?php echo yii::$app->homeUrl; ?>" class="btn btn-default">Back </a>
<?= Html::a('Delete', ['delete', 'id' => $view['id']], ['class' => 'btn btn-danger']) ?>
</div>
Answer
Solution:
Use checks if the specific key exists in your$view
data.
<div class="col-lg-4">
<h2><?= ($view['name'] ?? '') . ' ' . ($view['surname'] ?? '') ?></h2>
<h3><?= $view['title'] ?? ''; ?></h3>
<p><?= $view['body'] ?? ''; ?></p>
<a href="<?= Yii::$app->homeUrl; ?>" class="btn btn-default">Back</a>
<?php
if ($view) {
echo Html::a('Delete', ['delete', 'id' => $view['id']], ['class' => 'btn btn-danger'])
}
?>
</div>
EDIT:
You can basically check if you fetch$view
data in your controller also and respond to user if criteria conditions did not match any record from the database:
public function actionView($id)
{
$view = (new \yii\db\Query())
->select(['blog.id', 'user_id', 'body', 'title'])
->from('blog')
->join('INNER JOIN', 'users', 'blog.user_id = users.id')
->where(['blog.user_id' => $id])
->one();
if (!empty($view)) {
return $this->render('view', compact('view','id'));
}
throw new \yii\web\NotFoundHttpException('The requested blog does not exist.');
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function illuminate\encryption\openssl_cipher_iv_length()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.