php - 如何 if else 显示其他内容 laravel

标签 php html mysql database laravel

我的查看页面:

@if(empty($data))
  <p>No response have been attached to this entities.</p>
@else
  <p>By default, it will respond with the predefined phrases. Use the form below to customize responses.</p>
@endif

Controller :

public function queries($companyID, $entityType, $entityValue)
	{
		$data = [];

		$details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
		
		foreach ($details AS $datum) 
		{ 
			if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []]; 
			$data[$datum->intent]['question'][$datum->queries] = $datum->id; 
		} 

		$detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
		
		foreach ($detailsAns AS $datum) 
		{ 
			if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []]; 
			$data[$datum->intent]['answer'][$datum->reply] = $datum->id; 
		}

		ksort($data);
		return view('AltHr.Chatbot.queries', compact('data','entityType','entityValue','companyID'));
	}

我制作了上面显示的 Controller 和 View ,但是当没有数据时我似乎无法弄清楚问题是什么,它仍然显示如下:

enter image description here

我试图让它显示数据,但当没有数据时,它会显示其他内容。

我在dd()时有两个有数据和无数据的例子;

首先是数据:

with

第二个没有数据:

without

因此没有数据的应该显示其他内容,例如错误消息。

最佳答案

$data 在这两种情况下都不为空,您需要检查 answer 索引:

@if(empty($data['answer']))
  <p>No response have been attached to this entities.</p>
@else
  <p>By default, it will respond with the predefined phrases. Use the form below to customize responses.</p>
@endif

编辑

您还有一个包含 answerquestion 的空字符串索引,所以

@if(empty($data['']['answer']))

关于php - 如何 if else 显示其他内容 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48676539/

相关文章:

php - hasAndBelongsToMany 不提取关联外键的数据

javascript - 从 javascript 更改为 php/laravel 时,数组在后面加上 1

html - 有没有办法使用 VBA 转置 HTML 表格?

php - 将 mysql_query 结果传递给 foreach

MySQL查询一个类(class)的书籍成本

php - mysql + php编码

php - SQL 查询 - 获取行退出其他表 laravel 上的一些值

javascript - 如何使用 javascript 更改标签

javascript - 限制外部 CSS 对本地 React 组件或 HTML 元素的影响

mysql - 是否可以基于 MySQL 中某一列的选择进行插入?