如何以其他方式展示其他laravel

时间:2018-02-08 02:29:27

标签: 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

控制器:

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'));
	}

我制作了上面显示的控制器和视图,但我似乎无法弄清楚当没有数据时它仍然显示的问题:

enter image description here

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

当我dd();

时,我有两个有和没有数据的例子

首先是数据:

with

秒没有数据:

without

所以没有数据的那个应该显示其他类似错误信息。

2 个答案:

答案 0 :(得分:0)

因为你使用empty()函数来检查数据,但是在控制器$ data中是数组,所以它总是不为空。 您可以将empty()函数更改为count()函数。如果$ data为空或为null,则它将等于零。

&#13;
&#13;
@if(count($data)==0)
<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
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在这两种情况下,

$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']))