未定义的变量:属性(视图:error laravel 5


Undefined variable: property (View:error laravel 5

我正试图将一个变量传递给视图以进行输出,这样做会引发以下错误:未定义的变量:属性(视图:

我知道错误是因为视图中没有拾取变量,但我不知道为什么这里是我的代码

定义$property的控制器:

<?php
namespace App'Http'Controllers;
use Datatable;
use View;
use App'Models'EC2Instance;
use App'Models'ChangedProperties;
use Illuminate'Support'Facades'Input;
use App'Models'ChangelogItem;
class DetailsController extends Controller {
    protected $layout = 'ec2_instance';
    public function changedproperties($resource_id, $resource_type) {
        $changed_property = new ChangedProperties;
        $changed_properties = $changed_property->where('resource_id', $resource_id)->where('resource_type', $resouce_type);
    }
    public function details($instance_id) {
        $instance = new EC2Instance;
        $instance_obj = $instance->where('instance_id', $instance_id)->first();
        $property = new ChangelogItem;
        $properties = $property->where('resource_id', $instance_obj->id)->where('resource_type', 'ec2instance');
        //
        $table = Datatable::table()
                ->addColumn(
                        'Changed Property', 'Change Type', 'Previous Value', 'Updated Value'
                )
                ->setUrl('/ec2_instance/ec2instance/' . $instance_obj->id)
                ->noScript();


        return View::make('ec2_instance')->with('instance', $instance_obj, $properties)->with('table', $table);
    }
    public function instance_details($resource_type, $resource_id) {

        $query = ChangelogItem::select(array('message_id',
                            'type',
                            'changelog_item_id',
                            'created_at',
                            'updated_at',
                            'description'))
                        ->where('resource_id', $resource_id)->where('resource_type', $resource_type)->get();

        return Datatable::collection($query)
                        ->addColumn('message_id', function($model) {
                            $link2 = "<a href = '/changed_properties/" . $model->message_id . "' >" . $model->message_id . "</a>";
                            return $link2;
                        })
                        ->showColumns(
                                'message_id', 'type', 'changelog_item_id', 'created_at', 'updated_at', 'description')
                        ->searchColumns('message_id', 'type', 'changelog_item_id', 'created_at', 'updated_at', 'description')
                        ->orderColumns('message_id', 'type', 'changelog_item_id', 'created_at', 'updated_at', 'description'
                        )
                        ->make();
    }
}

并查看

<!doctype html>
<html lang="en">
    <head>
        <title>Instance Details </title>

        {!!HTML::style("css/jquery.dataTables.css")!!}
        {!!HTML::script("js/jquery.js")!!}
        {!!HTML::script("js/jquery.dataTables.min.js")!!}
        <meta charset="UTF-8">
        <title>Config Search</title>
        <style>
            @import url(//fonts.googleapis.com/css?family=Raleway:700);

            body {
                margin:0;
                font-family:'Raleway', sans-serif;
                text-align:center;
                color: #3a3a3d;
            }
            .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>
    </head>
    <body>
        <div class="navigation-left" style="float:left">
            <a href="{!! url('/') !!}">Back  </a>
        </div>




        <div class="row">
            <div class="col-md-12">
                <h3>Instance Details {!! $instance->instance_id !!}</h3>

                <script language="Javascript">
                    function formatAsUKDate(date) {
                        var date = new Date();
                        var day = padWithZero(date.getDate(), 2);
                        var month = padWithZero(date.getMonth() + 1, 2);
                        var year = date.getFullYear();
                        var hour = date.getHours();
                        if (hour <= 9)
                            hour = '0' + hour;
                        var mins = date.getMinutes();
                        if (mins <= 9)
                            mins = '0' + mins;

                        return  day + '/' + month + '/' + year + ' ' + hour + ':' + mins;
                    }
                    function padWithZero(str, minLength) {
                        str = String(str);
                        while (str.length < minLength) {
                            str = '0' + str;
                        }
                        return str;
                    }
                    document.write("last refreshed: " + formatAsUKDate(document.lastModified) + "");
                </SCRIPT>
            </div>
            <table>
                <tr><th>Instance ID</th><td>{!! $instance->instance_id !!}</td></tr>
                <tr><th>Image ID</th><td>{!! $instance->image_id !!}</td></tr>
                <tr><th>Private DNS Name</th><td>{!! $instance->private_dns_name !!}</td></tr>
                <tr><th>Public DNS Name</th><td>{!! $instance->public_dns_name !!}</td></tr>
                <tr><th>Key Name</th><td>{!! $instance->key_name !!}</td></tr>
                <tr><th>Instance Type</th><td>{!! $instance->instance_type !!}</td></tr>
                <tr><th>Launch Time</th><td>{!! $instance->launch_time !!}</td></tr>
                <tr><th>Kernel ID</th><td>{!! $instance->kernel_id !!}</td></tr>
                <tr><th>Subnet ID</th><td>{!! $instance->subnet_id !!}</td></tr>
                <tr><th>VPC ID</th><td>{!! $instance->vpc_id !!}</td></tr>
                <tr><th>Private IP Address</th><td>{!! $instance->private_ip_address !!}</td></tr>
                <tr><th>Public IP Address</th><td>{!! $instance->public_ip_address !!}</td></tr>
                <tr><th>Architecture</th><td>{!! $instance->architecture !!}</td></tr>
                <tr><th>Root Device Type</th><td>{!! $instance->root_device_type !!}</td></tr>
                <tr><th>Root Device Name</th><td>{!! $instance->root_device_name !!}</td></tr>
                <tr><th>Virtualization Type</th><td>{!! $instance->virtualization_type !!}</td></tr>
                <tr><th>Source Dest Check</th><td>{!! $instance->source_dest_check !!}</td></tr>

                </table>
        </div>
        <table
    <tr><th>Message ID</th><td>{!! $property->message_id !!}</td></tr>
                <tr><th>Type</th><td>{!! $property->type !!}</td></tr>
                <tr><th>Changelog Item ID</th><td>{!! $property->changelog_item_id !!}</td></tr>
                <tr><th>Created</th><td>{!! $property->created_at !!}</td></tr>
                <tr><th>Updated</th><td>{!! $property->updated_at !!}</td></tr>
                <tr><th>Description</th><td>{!! $property->description !!}</td></tr>
        </table>
         {!! $table->render() !!}
                {!! $table->script() !!}



    </body>
</html>

已找到$instance,但未找到$property。

这是因为您没有在视图中传递它看看这里

return View::make('ec2_instance')->with('instance', $instance_obj, $properties)->with('table', $table);

我认为它应该像这个

return View::make('ec2_instance')
       ->with('instance', $instance_obj)
       ->with('property', $property)
       ->with('table', $table);

此外,你似乎并没有从这里的数据库中获取一些东西

$properties = $property->where('resource_id', $instance_obj->id)->where('resource_type', 'ec2instance');

如果您希望实例返回,则此查询应以first()方法结束

如果希望返回集合,则为get()