laravel变量不从mysql阅读

1hdlvixo  于 2024-01-05  发布在  Mysql
关注(0)|答案(1)|浏览(239)

我回到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

  1. <x-section>
  2. <x-subtitle subTitle="Areas of Speciality" />
  3. <x-jobs :jobs="$jobs"/>
  4. </x-section>

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

  1. @foreach ($jobs as $job)
  2. <div class="tile is-parent is-4">
  3. <div class="tile is-child">
  4. <h4 class="is-size-4 has-text-weight-semibold my-2">
  5. <span class="icon is-purple mr-4"><i class="{{ $job->icon }} "></i></span><br>
  6. {{ $job->jobTitle }}
  7. <div class="divider my-2"></div>
  8. </h4>
  9. <p class="is-size-6 is-wordy"> {{ $job->description }} </p>
  10. </div><!-- is-child -->
  11. </div><!-- is-parent -->
  12. @endforeach


app/View/Components/Jobs.php

  1. <?php
  2. namespace App\View\Components;
  3. use Closure;
  4. use Illuminate\Contracts\View\View;
  5. use Illuminate\View\Component;
  6. class Jobs extends Component
  7. {
  8. /**
  9. * Create a new component instance.
  10. */
  11. public function __construct()
  12. {
  13. //
  14. }
  15. /**
  16. * Get the view / contents that represent the component.
  17. */
  18. public function render(): View|Closure|string
  19. {
  20. return view('components.jobs');
  21. }
  22. }


感谢耐心

jgzswidk

jgzswidk1#

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

  1. <?php
  2. namespace App\View\Components;
  3. use Closure;
  4. use Illuminate\Contracts\View\View;
  5. use Illuminate\View\Component;
  6. class Jobs extends Component
  7. {
  8. public $jobs;
  9. /**
  10. * Create a new component instance.
  11. */
  12. public function __construct($jobs)
  13. {
  14. $this->jobs = $jobs;
  15. }
  16. /**
  17. * Get the view / contents that represent the component.
  18. */
  19. public function render(): View|Closure|string
  20. {
  21. return view('components.jobs');
  22. }
  23. }

字符串

展开查看全部

相关问题