【问题标题】:Laravel: Undefined variable $test in production but runs fine locallyLaravel:生产中未定义的变量 $test 但在本地运行良好
【发布时间】:2021-06-14 20:54:12
【问题描述】:

我正在尝试在刀片组件中呈现一个简单的对象数组。我的代码在本地机器上运行良好,但在生产中抛出“未定义变量”错误。我正在使用 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

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

【问题讨论】:

  • 哪个变量未定义?
  • @StefanPavlov: $test
  • 我们看不到您如何将控制器中的测试变量传递给视图。
  • @StefanPavlov:这不是我会通过它的方式吗?如果不是,为什么这会在我的本地机器上工作? foreach ($test as $key => $item)

标签: php laravel laravel-blade


【解决方案1】:

改变这个

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

到这里

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

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

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

@if (isset($test))

【讨论】:

  • 进行了您建议的更改,但我仍然得到以下信息:[2021-03-17 15:54:51] production.ERROR: Undefined variable: test {"exception":"[object ] (Facade\\Ignition\\Exceptions\\ViewException(code: 0): Undefined variable: test 我没有收到错误,我可以在本地正确加载页面。
  • 在返回语句之前尝试在刀片中使用 {{ dd($test) }} 转储变量,并在控制器中使用 dd($this->test()) 转储变量。您还可以将 @if(isset($test)) 放在刀片中的 foreach 循环之前
猜你喜欢
  • 2013-02-12
  • 2017-08-06
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
相关资源
最近更新 更多