Задать вопрос
@scronheim

Неизвестная переменная в laravel 5.3?

Всем привет. в albums/add.blade.php есть такой код

@foreach($year as $item)
<option>{{ $item->name }} </option>
@endforeach


в контроллере код

$year = DB::table('year')->orderBy('name', 'desc')->get();
return view('albums.add', ['year' => $year]);

А вьюха выдает ошибку undefined variable $year. Что я делаю не так?

Undefined variable: year (View: D:\xampp\htdocs\coldmusic\resources\views\albums\add.blade.php)
in a12e35343628b5a244f46b67e5704183d5c95b8c.php line 18
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('D:\xampp\htdocs\coldmusic\storage\framework\views/a12e35343628b5a244f46b67e5704183d5c95b8c.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'currentPath' => 'albums/add')) in CompilerEngine.php line 59
at CompilerEngine->get('D:\xampp\htdocs\coldmusic\resources\views/albums/add.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'currentPath' => 'albums/add')) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 45
at Response->setContent(object(View)) in Response.php line 201
at Response->__construct(object(View)) in Router.php line 1028
at Router->prepareResponse(object(Request), object(View)) in Router.php line 653
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 655
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 629
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 53
at require_once('D:\xampp\htdocs\coldmusic\public\index.php') in server.php line 21
  • Вопрос задан
  • 293 просмотра
Подписаться 1 Оценить Комментировать
Пригласить эксперта
Ответы на вопрос 2
У вас точно роут обрабатывается в контроллере, а не отдается вьюха напрямую из файла роутов? В логе ошибки никакой контроллер не упомянут.
Ответ написан
Комментировать
codeschemer
@codeschemer
Пробовал в контроллере?
dd($year);
Если результат
Collection {#275 ▼
  #year: []
}

тогда у тебя пустая коллекция.
Ответ написан
Комментировать
Ваш ответ на вопрос

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

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