laravel变量不从mysql阅读

1hdlvixo  于 4个月前  发布在  Mysql
关注(0)|答案(1)|浏览(33)

我回到Laravel项目,因为我没有编码来处理个人情况。现在我撞到了一堵墙。我得到了一个“未定义的变量”错误,我不知道如何修复。这在两个月前工作得很好,我不明白为什么它现在不工作了。
当我从组件中删除@foreach和MySQL阅读时,错误消息消失了,剩下的是空模板。我认为这是从数据库中阅读的问题。
下面是错误消息:
HTTP 500内部服务器错误
未定义变量$jobs(视图:/Users/macUser/Desktop/new-app/resources/views/pages/about.blade.php)
new-app/storage/framework/views/bf7b3742a5b2f32d56326224b3c2159e.php:110
withAttributes(“jobs”=> \Illuminate\View\BladeController::sanitizeControlAttribute($jobs)]);?>renderComponent();?>
代码如下:
views/pages/about.blade.php

<x-section>

    <x-subtitle subTitle="Areas of Speciality" />

    <x-jobs :jobs="$jobs"/>

</x-section>

字符串
views/components/jobs.blade.php

@foreach ($jobs as $job)

<div class="tile is-parent is-4">

    <div class="tile is-child">

        <h4 class="is-size-4 has-text-weight-semibold my-2">
            <span class="icon is-purple mr-4"><i class="{{ $job->icon }} "></i></span><br>
            {{ $job->jobTitle }}
            <div class="divider my-2"></div> 
        </h4>
        <p class="is-size-6 is-wordy"> {{ $job->description }} </p>

    </div><!-- is-child -->
</div><!-- is-parent -->

@endforeach


app/View/Components/Jobs.php

<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Jobs extends Component
{
    /**
     * Create a new component instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view('components.jobs');
    }
}


感谢耐心

jgzswidk

jgzswidk1#

在某种程度上,我理解为什么**$jobs**会给你错误,因为你没有接受你的组件类中的$jobs。
更新你的组件类并接受这个$jobs。

<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Jobs extends Component
{
    public $jobs;

    /**
     * Create a new component instance.
     */
    public function __construct($jobs)
    {
        $this->jobs = $jobs;
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view('components.jobs');
    }
}

字符串

相关问题