【问题标题】:how to if else to show something else laravel如果其他,如何显示其他东西 laravel
【发布时间】:2018-07-18 11:52:37
【问题描述】:

我的浏览页面:

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

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

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

当我 dd(); 时,我有两个有数据和没有数据的例子;

首先是数据:

第二个没有数据:

所以没有数据的应该显示错误消息之类的其他内容。

【问题讨论】:

    标签: php html mysql database laravel


    【解决方案1】:

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

    @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

    【讨论】:

      【解决方案2】:

      $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
      

      编辑

      你还有一个空字符串索引,同时包含 answer 和 question 所以

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

      【讨论】:

      • 我也是这样做的,还是不行,这样显示“

        没有响应已附加到这个实体。

        ”有数据和没有数据跨度>
      • 我已经编辑了我的答案,你之前有一个空字符串索引
      • 还是和我上面提到的一样
      • $datum-&gt;intent的目的是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-09
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多