php 未定义变量:$ Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31264242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:20:30  来源:igfitidea点击:

Undefined variable: $ Laravel 5

phplaravel

提问by zlotte

This is the code from CategoriesController:

这是来自 CategoriesController 的代码:

 public function index()
{
    $categories = Category::all();
    return view('/home', compact('categories'));
}

And this is the code that I'm writing to the home.blade.php

这是我写给 home.blade.php

                @foreach($categories as $cat)
                @endforeach

And then in home.blade.php

然后在 home.blade.php

I'm getting this error: Undefined variable: categories

我收到此错误: Undefined variable: categories

Why this happening ? Everything works fine with Articles but not categories and the code is the same.

为什么会这样?文章一切正常,但类别无效,代码相同。

Home.blade.php

首页.blade.php

@extends('app')

@section('content')

     <div class="col-md-4 pull-right">
      <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title">Categories</h3>
        </div>
        <div class="panel-body">

                          @foreach($categories as $cat)
                          <div class="col-lg-6">
                            <ul class="list-unstyled">
                                <li><a href="articles/category/{{  }}">News</a>
                                </li>
                            </ul>
                        </div>                          
                          @endforeach
   </div>
      </div>     
    </div>


  @if(count($articles))

    @foreach($articles as $article)

<div class="panel panel-default col-md-8">
  <div class="panel-heading">

  <h3>{{ $article->title }}</h3>
   <small>posted by <a href="users/{{ $article->author->name }}">{{ $article->author->name }}</a> {{ $article->created_at }}</small>
  </div>
  <div class="panel-body">
  {!! str_limit($article->body, 1000) !!} 
    <hr>
  <a href="{{ url('/articles/'.$article->slug) }}">Read More</a>   
  </div>
</div>

        @endforeach
    @else
        <h1>No articles at the moment.</h1>
    @endif
    <br>
    {!! $articles->render() !!}
@stop

回答by Ben Swinburne

You have two routes

你有两条路线

Route::get('/home', 'ArticlesController@index');
Route::get('/home', 'CategoriesController@index');

Which means when you visit /home, only ArticlesController@index is fired, not both.

这意味着当你访问 /home 时,只有 ArticlesController@index 被触发,而不是两者都被触发。

This means that the $categoriesvariable used in the view is not populated because your index()method in CategoriesController is intended to do this but is never called.

这意味着$categories未填充视图中使用的变量,因为index()CategoriesController 中的方法旨在执行此操作但从未调用过。

Therefore you need to do something like this

因此你需要做这样的事情

Route::get('/home', 'HomeController@index');

public function index()
{
    $categories = Category::all();
    $articles   = Article::all();

    return view('/home', compact('articles', 'categories'));
}

回答by Adnan Zafar

try to use

尝试使用

return View::make('/home')->with(compact('categories'));

instead of

代替

return view('/home', compact('categories'));

回答by anthony zorilla

I insert this in the top of my @foreach

我将它插入到 @foreach 的顶部

 <?php
     use App\models\PersonalContact;
     $data = PersonalContact::all();
 ?>

or this before @foreach add @if statement like this

或者在@foreach 之前添加这样的@if 语句

 @if (count($data) > 0)
            @foreach ($data as $bloginfo)
                <tr role="row" class="odd">
                    <td class="sorting_1">{{$bloginfo->name}}</td>
                    <td>{{$bloginfo->description}}</td>
                    <td>{{$bloginfo->email}}</td>
                    <td>{{$bloginfo->phone}}</td>
                    <td><img src="{{url('images/'.$bloginfo->image)}}" alt="" width="100px" height="100px"></td>
                <td><a href="{{url('admin/{admin}/personalcontact/create')}}" class="btn btn-app"><i class="fas fa-edit"></i>Add</a> | <a href="{{url('admin/{admin}/personalcontact/{personalcontact}/edit')}}" class="btn btn-app"><i class="fas fa-edit"></i>Edit</a> | <a class="btn btn-app"><i class="fas fa-edit"></i>Delete</a></td>
                </tr>
            @endforeach
        @endif    

回答by Cheda.M

Hi guys well in my case it was my database table name it was ( table_23) of which I kept on getting the undefined variable error then I changed it to (table23) and my problem was solved

大家好,在我的情况下,这是我的数据库表名(table_23),其中我一直收到未定义的变量错误,然后我将其更改为(table23)并且我的问题解决了