尝试在视图中显示图像:
id_image
在db.By中保存图像路径,使用路径需要显示图像。
试过以下代码,但没有显示任何图像。
<div class="row">
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="<?php $dataProvider->models[0]->id_image; ?>">
</a>
</div>
查看:
public function actionView($id)
{
$searchModel = new CreateBookingsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('view', [
'model' => $this->findModel($id),
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
在控制器中创建/上传文件
public function actionCreate()
{
$model = new CreateBookings();
if ($model->load(Yii::$app->request->post()))
{
$imageName = $model->first_name;
$mobile = $model->primary_mobile;
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs( 'uploads/id_images/'.$imageName.'_'.$mobile.'.'.$model->file->extension);
//save the path in the db column
$model->id_image = 'uploads/id_images/'.$imageName.'_'.$mobile.'.'.$model->file->extension;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
保存在db:
中的路径示例uploads/id_images/kumar_9015879992.jpg
答案 0 :(得分:2)
您必须将基本网址添加到图片路径。请使用以下网址。
<img src="<?php echo Yii::getAlias('@web').'/'.$dataProvider->models[0]->id_image; ?>">
答案 1 :(得分:0)
您希望在将其保存到数据库之前使用first_name名称我想,尝试一下
if ($model->load(Yii::$app->request->post()))
{
$model->save();
//then use first_name on your image like you did.
$model->save();
}