未定义的变量:ip_address(视图:
Undefined variable: ip_address (View:
我收到一个未声明的变量错误,但不明白为什么
我正在尝试实现全文搜索并具有以下代码
在我的控制器中
<?php
class PostsController extends BaseController {
public function postSearch(){
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
return View::make('ec2_instance', compact('ip_address'));
}
}
?>
在我看来
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel PHP Framework</title>
<style>
@import url(//fonts.googleapis.com/css?family=Raleway:700);
body {
margin:0;
font-family:'Raleway', sans-serif;
text-align:center;
color: #999;
}
.welcome {
width: 300px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -100px;
}
a, a:visited {
text-decoration:none;
}
h1 {
font-size: 32px;
margin: 16px 0 0 0;
}
</style>
<div class="search">
{{ Form::model(null, array('route' => array('search'))) }}
{{ Form::text('query', null, array( 'placeholder' => 'Search query...' )) }}
{{ Form::submit('Search') }}
@foreach($posts as $post)
<li>{{ $post->instance_id }}</li>
<li>{{ $post->instance_type }}</li>
<li>{{ $post->availability_zone }}</li>
<li>{{ $post->status_checks }}</li>
<li> {{ $post->alarm_status }}</li>
<li> {{ $post->public_dns }}</li>
<li>{{ $post->key_name }}</li>
@endforeach
@foreach($ip_address as $pip)
<li>{{ $pip->ip_address }}</li>
@endforeach
{{ Form::close() }}
我这辈子都看不出错字
您不能有 2 个 return 语句。
return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
return View::make('ec2_instance', compact('ip_address'));
试试这个:
public function postSearch(){
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
return View::make('ec2_instance', compact('ip_address', 'posts'));
}
在你的postSearch
方法中
public function postSearch()
{
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
return View::make('ec2_instance', compact('ip_address'));
}
您有 两个 return 个陈述
return View::make('ec2_instance', compact('posts'));
///...
return View::make('ec2_instance', compact('ip_address'));
第一个 return 语句之后的代码永远不会运行,并且永远不会将 $ip_address
变量发送到视图。像
public function postSearch()
{
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
//return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
//return View::make('ec2_instance', compact('ip_address'));
return View::make('ec2_instance', [
'ip_address'=>$ip_address,
'posts'=>$post
]);
}
可能就是您想要的。
我收到一个未声明的变量错误,但不明白为什么
我正在尝试实现全文搜索并具有以下代码
在我的控制器中
<?php
class PostsController extends BaseController {
public function postSearch(){
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
return View::make('ec2_instance', compact('ip_address'));
}
}
?>
在我看来
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel PHP Framework</title>
<style>
@import url(//fonts.googleapis.com/css?family=Raleway:700);
body {
margin:0;
font-family:'Raleway', sans-serif;
text-align:center;
color: #999;
}
.welcome {
width: 300px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -100px;
}
a, a:visited {
text-decoration:none;
}
h1 {
font-size: 32px;
margin: 16px 0 0 0;
}
</style>
<div class="search">
{{ Form::model(null, array('route' => array('search'))) }}
{{ Form::text('query', null, array( 'placeholder' => 'Search query...' )) }}
{{ Form::submit('Search') }}
@foreach($posts as $post)
<li>{{ $post->instance_id }}</li>
<li>{{ $post->instance_type }}</li>
<li>{{ $post->availability_zone }}</li>
<li>{{ $post->status_checks }}</li>
<li> {{ $post->alarm_status }}</li>
<li> {{ $post->public_dns }}</li>
<li>{{ $post->key_name }}</li>
@endforeach
@foreach($ip_address as $pip)
<li>{{ $pip->ip_address }}</li>
@endforeach
{{ Form::close() }}
我这辈子都看不出错字
您不能有 2 个 return 语句。
return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
return View::make('ec2_instance', compact('ip_address'));
试试这个:
public function postSearch(){
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
return View::make('ec2_instance', compact('ip_address', 'posts'));
}
在你的postSearch
方法中
public function postSearch()
{
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
return View::make('ec2_instance', compact('ip_address'));
}
您有 两个 return 个陈述
return View::make('ec2_instance', compact('posts'));
///...
return View::make('ec2_instance', compact('ip_address'));
第一个 return 语句之后的代码永远不会运行,并且永远不会将 $ip_address
变量发送到视图。像
public function postSearch()
{
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
//return View::make('ec2_instance', compact('posts'));
$ip_address = public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
//return View::make('ec2_instance', compact('ip_address'));
return View::make('ec2_instance', [
'ip_address'=>$ip_address,
'posts'=>$post
]);
}
可能就是您想要的。