当前位置: 首页 > 知识库问答 >
问题:

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

高增
2023-03-14

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

同样,这在本地工作非常好。只有当我在生产中访问我的站点时才抛出错误。感谢任何帮助。

共有1个答案

孔理
2023-03-14

更改此

return view('components.timeline');

对此

return view('components.timeline')->with('test', $this->test());

如果您没有在其他地方使用它,那么您也可以将测试方法设置为私有的。

 类似资料:
  • 我开发了一个React应用程序,它使用Laravel作为API。我已经通过Passport添加了登录,并且已经相当成功地使用了个人访问客户端方法。我可以添加新用户和令牌,我可以撤销令牌,我可以重置密码...所有的API调用(除了登录和注册)都由API中间件保护,它可以工作。如果我从任何这些调用的头中删除,则由于->middleware('auth:api')包装器,它将返回401未经验证。 一切都

  • 我在Heroku上托管我的express API,在Netlify上托管我的客户端。当我在本地尝试我的注册路径时,我的cookie被定义并且该路径工作。然而,当它在生产中时,cookie总是返回未定义的,并且MyAPI超时。 请注意,cookie已从后端成功发送。我可以在开发工具中查看它。另外,cookie, get()返回一个空对象。 我正在使用Js-cookie。 我在盖茨比使用js cook

  • 我已经使用Appium和Java以及TestNG为Android应用程序编写了一些测试。我的测试在模拟器上本地运行良好,但当我将它们上传到设备场时,我的测试总是失败。我已经尽可能地简化了我的测试,但它仍然不起作用。目前,我尝试运行的测试会在打开应用程序时出现的提示中找到并单击“允许权限”按钮。代码: 观看Device Farm提供的视频,我可以看到它打开并立即关闭我的应用程序。它甚至没有像应该的那

  • 在laravel中,没有定义常量文件,所以我继续寻找一种实现常量使用的方法。下面的方法是我设法组合起来的: 现在,我的问题是;在我看来,有没有更干净的方法来检索我的常量?类似的东西: 这是为了保持我的观点好,短和干净。 感谢您的投入!

  • 问题内容: 在jQuery的核心风格指南建议两种不同的方法来检查一个变量是否被定义。 全局变量: 局部变量: 特性: 为什么jQuery为什么对全局变量使用一种方法而对局部变量和属性使用另一种方法? 问题答案: 对于未声明的变量,将返回字符串文字,而身份检查将触发错误 “未定义foo” 。 对于局部变量(您 知道 已在某处声明),不会发生此类错误,因此进行身份检查。

  • 我在copris v.1.1中使用Akari求解器 这是scalademoapp.scala代码: 尝试执行求解器时会引发此异常: 线程“main”java中出现异常。lang.RuntimeException:目标变量未在jp定义。神户杯。糖解算器。findOptBody(Sugar.scala:631)在jp。神户杯。AbstractSolver$$anonfun$findOpt$1。在jp处