未定义的变量:ip_address(查看:

时间:2015-01-02 12:29:24

标签: php laravel

我收到一个未声明的变量错误,但看不出原因

我正在尝试实施全文搜索并拥有以下代码

在我的控制器中

<?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 个答案:

答案 0 :(得分:1)

你不能有2个回复陈述。

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



}

答案 1 :(得分:0)

使用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 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
   ]);
}

可能就是你想要的。