微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel:生产中未定义变量 $test 但在本地运行良好

如何解决Laravel:生产中未定义变量 $test 但在本地运行良好

我正在尝试在刀片组件中渲染一个简单的对象数组。我的代码在我的本地机器上运行良好,但在生产中抛出“未定义变量”错误。我正在使用 Laravel Forge 和 Digital Ocean 来为我的网站提供服务。 PHP 版本在两种环境中都匹配。

timeline.PHP

<?PHP

namespace App\View\Components;

use Illuminate\View\Component;

class timeline extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|string
     */
    public function render()
    {
        return view('components.timeline');
    }

    public function test()
    {
        return [
            [
                'year' => '1826','desc' => "Built during 1826 and 27,the Cascade Locks became the site of the city's first industrial valley.
                            The same topography that presented an obstacle for the canal builders provided waterpower for a string of
                            industries that soon lined the canal. This new village,founded by Dr. Eliakim Crosby,with help from Simon Perkins,was called Cascade. It later became what we Now call north Akron. The Cascade Locks were constructed of huge blocks of sandstone,sawed and chiseled to shape. The locks have a width of 15 feet,and they are up to 90 feet long. The source of
                            canal water was,and still is the Portage Lakes.",'img' => 'cascade-valley-1882.jpg','alt' => 'opening Day'
            ],[...],
timeline.blade.PHP

<!-- component -->
<div class="container bg-green-700 bg-opacity-10 mx-auto">
    <div class="relative wrap py-8 px-4 lg:p-10">
        @foreach ($test as $key => $item)
            <div class="mb-8 flex justify-between flex-col-reverse
                {{ $key % 2 == 0 ? 'lg:flex-row-reverse' : 'lg:flex-row' }}
                items-center w-full right-timeline pb-4">
                <div class="w-5/12"></div>
                <div class="z-20 flex items-center order-1 bg-green-700 bg-opacity-30 shadow-xl p-3 rounded-full mb-4 lg:mb-0">
                    <h1 class="mx-auto font-semibold text-lg text-black">{{ $item['year'] }}</h1>
                </div>
                <div class="bg-green-800 text-white rounded-lg shadow-xl w-full lg:w-5/12 px-6 py-4"
                    style="text-shadow: 0px 0.25px white;">
                    <h3 class="mb-3 font-extrabold text-xl">Headline</h3>
                    <p class="text-sm leading-snug tracking-wide text-opacity-100 mb-4">
                        {{ $item['desc'] }}
                    </p>
                    <img src="{{ URL::to('/') }}/images/pages/history/{{ $item['img'] }}" alt="{{ $item['alt'] }}"
                        class="m-auto mb-2 w-full h-72 object-cover object-center rounded-md" />
                </div>
            </div>
        @endforeach
    </div>
</div>
history.blade.PHP

@extends('layouts.default')

@section('content')

    <div id="history" class="h-80 w-full relative flex flex-col items-center justify-center">
        <div class="absolute">
            <img src="{{ URL::to('/') }}/images/pages/history/canal-timeline.png" alt="Canal Timeline Icon"
                class="object-cover object-center" style="z-index: 1;" />
        </div>
        <div class="h-1/2 w-full bg-gray-100"></div>

        <div class="h-1/2 w-full bg-green-700 bg-opacity-20"></div>
    </div>

    <div class="pt-6 pb-6 lg:pb-14 bg-green-700 bg-opacity-20">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center">
                <p class="mt-2 text-3xl leading-8 font-extrabold tracking-tight text-gray-900 sm:text-4xl">
                    Cascade Locks History
                </p>
            </div>
        </div>
    </div>


    <x-timeline />


    <script>
        $(document).ready(function() {
            // Handler for .ready() called.
            $('html,body').animate({
                scrollTop: $('#history').offset().top
            },'slow');
        });

    </script>
@endsection

同样,这在本地工作得很好。只有当我在生产中访问我的网站时才会抛出错误。任何帮助表示赞赏。

解决方法

改变这个

public function render()
{
    return view('components.timeline');
}

到这里

public function render()
{
    return view('components.timeline')->with('test',$this->test());
}

如果您不在其他任何地方使用它,您也可以将测试方法设为私有。

你也可以把这个放在 foreach 之前

@if (isset($test))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。