为不同的函数获取错误未定义的变量

时间:2018-02-13 03:50:05

标签: php database laravel

您好我有一个页面,我从数据库调用数据来查看它。在那个页面上,我已经完成了一个搜索功能,如下所示。但在创建主函数后我有错误:未定义的变量:db。我不明白为什么当搜索功能是一个不同的功能时它会寻找'db'?

我的刀片文件:

pass %>% 
  mutate(scoring_chance = if_else(is.na(scoring_chance), "0", "1"),
         ...)

mainfunction:

<div class="container-fluid container-fixed-lg">
	<div class="row">
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">
					<div class="panel-title">Answer Few Questions</div>
					<div class="clearfix"></div>
				</div>
				<div class="panel-body">
					<div class="form-group">
						<div class="col-md-12">

							

							@foreach($db as $intentName => $questionAnswer)

							<?php
							$number = 0;
							?>
							
				            <div class="form-group edit-response-container">
				              <label data-toggle="collapse" data-target="#collapse-{{$number}}" aria-expanded="false" aria-controls="collapse-{{$number}}"> {{$intentName}} <i class="fa fa-chevron-down"></i></label>
				              <div class="collapse" id="collapse-{{$number}}">
				                <div class="well">
									<form action="{{action('AltHr\Chatbot\ChatbotController@savedmb', [$companyID, $entityType])}}" method="post">
					        		{{csrf_field()}}
										<div class="col-md-12 btn-toolbar">
											<button type="button" class="btn alt-btn alt-btn-green pull-right add-new">Add Answer</button>
											<button type="submit" class="btn alt-btn alt-btn-green pull-right">Save</button>
										</div>

										@foreach($questionAnswer['q1'] as $question => $questionID)
							              <table class="question">
							                <tr>
							                  <td class="labels"><p>User:</p></td>
							                  <td><p>&nbsp;{{$question}}</p></td>
							                </tr>
							              </table>
							            @endforeach

							            @foreach($questionAnswer['q2'] as $question => $questionID)
							              <table class="question">
							                <tr>
							                  <td class="labels"><p>User:</p></td>
							                  <td><p>&nbsp;{{$question}}</p></td>
							                </tr>
							              </table>
							            @endforeach

							            @foreach($questionAnswer['q3'] as $question => $questionID)
							              <table class="question">
							                <tr>
							                  <td class="labels"><p>User:</p></td>
							                  <td><p>&nbsp;{{$question}}</p></td>
							                </tr>
							              </table>
							            @endforeach
							            <br><br>

										<div class="new-field">
											<table class="response">
												<tr>
													<td class="labels"><p>Answer:</p></td>
													<td><input type="text" name="newAnswer[]" value=""></td>
												</tr>
											</table>
										</div>


							        </form>
				                </div>
				              </div>
				            </div>

				            <?php
								$number++;
							?>

				            @endforeach


						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

搜索功能:

	public function addType($companyID, $entityType)
	{
		$data = [];
		$intentTypes = DiraQuestion::select('intent')->where('company_id', $companyID)->groupBy('intent')->get();

		foreach ($intentTypes as $intent) 
		{
			$data[$intent->intent] = $intent->intent;
		}



		$db = [];
		$dmb = DiraMasterBot::all();
		foreach ($dmb AS $datum) 
		{ 
			if (!isset($db[$datum->description])) $db[$datum->description] = ['intent' => [], 'q1' => [], 'q2' => [], 'q3' => []]; 
			$db[$datum->description]['intent'][$datum->intent] = $datum->id; 

			if (!isset($db[$datum->description])) $db[$datum->description] = ['intent' => [], 'q1' => [], 'q2' => [], 'q3' => []]; 
			$db[$datum->description]['q1'][$datum->sampleQ1] = $datum->id; 

			if (!isset($db[$datum->description])) $db[$datum->description] = ['intent' => [], 'q1' => [], 'q2' => [], 'q3' => []]; 
			$db[$datum->description]['q2'][$datum->sampleQ2] = $datum->id; 

			if (!isset($db[$datum->description])) $db[$datum->description] = ['intent' => [], 'q1' => [], 'q2' => [], 'q3' => []]; 
			$db[$datum->description]['q3'][$datum->sampleQ3] = $datum->id; 
		} 

		ksort($db);
		// dd($db);


		return view('AltHr.Chatbot.addType', compact('db','data','entityType','companyID'));
	}

1 个答案:

答案 0 :(得分:0)

因为您返回了'AltHr.Chatbot.addType'的相同视图。因此,在该视图文件中,您在$db循环中使用foreach变量。您应该检查在$db循环之前是否设置了foreach变量,或者返回没有$db变量的其他视图。