@Zumix

Почему Undefined variable в Laravel?

Есть компонент :
<?php

namespace App\Http\Livewire;

use Carbon\CarbonPeriod;
use Livewire\Component;
use App\Models\Calendar as ScheduleModel;

class CalendarComponent extends Component
{
    public $items;
    public $days;
    public $times = [];

    public function mount() {
        $items = ScheduleModel::today()->get();

        foreach ($items as $item) {
            $this->items[now()->parse($item->date)->format("Y-m-d")][$item->time->hour][] = $item;
        }

        $this->days = CarbonPeriod::create(now()->startOfWeek(), now()->endOfWeek())->toArray();

        foreach (range(8, 21) as $time) {
            $this->times[$time] = now()->parse("{$time}:00");
        }
    }

    public function render()
    {
        $this->days = array(1,2,3);
        return view('livewire.schedule') ;
    }
}

Есть модель :
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Calendar extends Model
{
    protected $fillable = ["name", "time", "date"];

    protected $appends = [ onAir', 'isPast'];

    protected $dates = ["date"];

    public function scopeToday(Builder $query)
    {
        return $query->whereDate("date", now());
    }

    public function getTimeAttribute($value) {
        return now()->parse($value);
    }

    public function getDateAttribute($value) {
        return now()->parse($value)->format("Y-m-d");
    }

    public function getIsPastAttribute() {
        return $this->isPast();
    }

    public function getOnAirAttribute() {
        return $this->isOnAir();
    }

    public function isPast() {
        return !$this->isOnAir() && now()->parse("{$this->date} {$this->time->format("H:i")}")->isPast();
    }

    public function isOnAir() {
        return now()->isBetween(
            now()->parse("{$this->date} {$this->time->format("H:i")}"),
            now()->parse("{$this->date} " . now()->parse($this->time)->addHour()->format("H:i"))
        );
    }
}

Есть view
@foreach($days as $day)
                            <th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-bold text-gray-500 uppercase tracking-wider {{ $day->isToday() ? 'border-l-2 border-r-2 border-t-2 border-blue-500' : 'border-b border-gray-200' }}">
                                <span class="text-black">{{ $day->dayName }}</span> {!!  $day->isToday() ? '<span class="lowercase">('.__('date.today').')</span>' : '' !!}
                                <strong class="text-black block">{{ $day->format("d") }} {{ $day->monthName }}</strong>
                            </th>
                        @endforeach

И есть роут :
Route::get('schedule/list/page', [\App\Http\Livewire\CalendarComponent::class, 'render'])->middleware('auth')->name('schedule/list/page');

При попытке вывода выходит ошибка Undefined variable $days в цикле @foreach($days as $day) ( во View
  • Вопрос задан
  • 158 просмотров
Пригласить эксперта
Ответы на вопрос 1
nokimaro
@nokimaro
Меня невозможно остановить, если я смогу начать.
нужно передать данные во view

<?php
return view('livewire.schedule', [
    'days' => [1, 2, 3, 4]
]) ;
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы